This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Addition is not working in JavaScript
So I've been working on a calculator of sorts for a game I play. I have the formula below to work it out, however, it's stringing the values together instead of actually adding them. So, if I put nothing in, I get "000000000000000000000000" for the value, and if I change totalattack to a 1 I get "10000000000000000000000". Having looked around a bit, I'm really not sure why it's doing that. Or how to fix it.
var invasionattack = totalattack + (600*DSC) + (60*translator) + (35*mindcontrol) + (30*psionic) + (40*mutagen) + (2500*cartridge) + (300*stryll) + (15*mech) + (20*bane) + (30*cbane) + (60*hbane) + (45*obane) + (75*mbane) + (decimator*200);
alert('Invasion Attack From Modules: ' + invasionattack);
If anyone is curious/if it's relevant, the full code can be found here:
http://glcalc.x10.mx/invasioncalc.html then view source.
There's also a bug with the multipliers somewhere, but I'll find that later.
String concatenation is not the same as addition. You need to make sure you're dealing with numbers, not strings.
That said, there are a number of other potential pitfalls with your calculatechance function .
You are declaring variables when you need them and then redeclaring variables later all of which will get you into trouble because of variable hoisting.
You are using "Truthy and Falsy" values, which in itself is not necessarily a bad thing and in fact (IMHO, one of the beautiful parts of JavaScript), but unless you understand how it works you can get into trouble with it.
You seem to be using anonymous code blocks:
{
if (decimator == 0) {
var decimatormult = 1;
};
if (!decimator == 0) for (var decimatormult = 1; decimator > 0; decimator--) {
decimatormult * 1.07
}
}
and I'm not sure how different browsers will interpret that. It would be better to eliminate them and use comments to delineate sections.
You're overwriting values for totalattack when you probably don't want to.
All said, your function may be better written as:
var calculatechance = function calculatechance() {
'use strict';
var bane = parseFloat(document.getElementById('bane').value, 10) || 0, // Convert to a number (float) and default to 0 if parsing fails.
cbane = parseFloat(document.getElementById('cbane').value, 10) || 0,
obane = parseFloat(document.getElementById('obane').value, 10) || 0,
hbane = parseFloat(document.getElementById('hbane').value, 10) || 0,
mbane = parseFloat(document.getElementById('mbane').value, 10) || 0,
exotic = parseFloat(document.getElementById('exotic').value, 10) || 0,
decimator = parseFloat(document.getElementById('decimator').value, 10) || 0,
mindcontrol = parseFloat(document.getElementById('mindcontrol').value, 10) || 0,
translator = parseFloat(document.getElementById('translator').value, 10) || 0,
anubix = parseFloat(document.getElementById('anubix').value, 10) || 0,
attack = parseFloat(document.getElementById('attack').value, 10) || 0,
// Calculate Invasion Attack
anubixattack = anubix === 100 ? 1 : 0, // Use of ternary operator
// Check Checkboxes
obelisk = document.getElementById("obelisk").checked ? 1 : 0, // Use of ternary operator with truthy/falsy as .checked equals "checked" (truthy) or "" (falsy)
foci = document.getElementById("foci").checked ? 1 : 0,
amp = document.getElementById("amp").checked ? 1 : 0,
overcharge = document.getElementById("overcharge").checked ? 1 : 0,
crux = document.getElementById("crux").checked ? 1 : 0,
mech = document.getElementById("mech").checked ? 1 : 0,
DSC = document.getElementById("DSC").checked ? 1 : 0,
kulgox = document.getElementById("kulgox").checked ? 1 : 0,
terror = document.getElementById("terror").checked ? 1 : 0,
psionic = document.getElementById("psionic").checked ? 1 : 0,
mutagen = document.getElementById("mutagen").checked ? 1 : 0,
stryll = document.getElementById("stryll").checked ? 1 : 0,
cartridge = document.getElementById("cartridge").checked ? 1 : 0,
// Other variables
exoticatt = 0,
decimatormult = 1,
totalattack = attack,
invasionattack = 0;
// Calculate Exotic Bio Disruptor Multiplier
// no logic currently here
// Calculate Exotic Bio Disruptor Static IAttack
switch (exotic) {
case 0:
exoticatt = 0;
break;
case 1:
exoticatt = 250;
break;
case 2:
exoticatt = 350;
break;
default:
exoticatt = (100 * exotic) + 150;
break;
}
//Calculate Atmospheric Decimator Multiplier
if (decimator !== 0) {
while (decimator > 0) {
decimatormult *= 1.07;
decimator -= 1;
}
}
//Calculate Attack
if (obelisk) {
totalattack += attack * 1.1;
}
if (foci) {
totalattack *= 1.05;
}
if (amp) {
totalattack *= 1.15;
}
if (crux) {
totalattack *= 1.1;
}
if (overcharge) {
totalattack *= 1.08;
}
if (anubixattack) {
totalattack += attack * 1.03;
}
//Calculate Invasion Attack
invasionattack = (
totalattack
+ (600 * DSC)
+ (60 * translator)
+ (35 * mindcontrol)
+ (30 * psionic)
+ (40 * mutagen)
+ (2500 * cartridge)
+ (300 * stryll)
+ (15 * mech)
+ (20 * bane)
+ (30 * cbane)
+ (60 * hbane)
+ (45 * obane)
+ (75 * mbane)
+ (decimator * 200)
+ exoticatt
);
alert('Invasion Attack From Modules: ' + invasionattack.toString());
invasionattack = invasionattack * decimatormult;
if (kulgox) {
invasionattack *= 1.1;
}
if (terror) {
invasionattack *= 1.08;
}
alert('Invasion Attack: ' + invasionattack);
};
Related
I'm trying to validate in Javascript that a number is in increments of 50, if not, then throw a validation error. For example:
123 - invalid, can either be 100 or 150
272 - invalid, can either be 200 or 250 or 300
etc...
I'm thinking that the % remainder operator is what I need to use but not quite sure how to build a javascript validation rule to match this.
Are you looking for something like:
(val) => {
var remainder = val % 50;
if (remainder !== 0) {
var lower = val - remainder;
var higher = lower + 50;
throw new Error(val + ' - invalid, can either be ' + String(lower) + ' or ' + String(higher));
}
}
That could be reduced, but this way you can see the logic at work.
This is the math you want to preform:
Math.round(123/ 50)*50; //this gives 100
Math.ceil(123/ 50)*50; //this gives 150
and here is the validation function
function validate(number) {
var round = Math.round(number / 50) * 50;
var ceil = Math.ceil(number / 50) * 50;
if (number == round) { return console.log("valid"); }
return console.log(`${number} - invali, can eiher be ${round} or ${ceil}`);
}
I'm trying to create a calculator that when you input your points, and the total amount of points in the class you receive a lot of information about it, I currently have working the letter grade, percentage, and the amount of points that you need to get to the next grade up.
Ex. 95/100
A 95%
Points Needed = 5
I want to do the same thing with Points needed but opposite. So with the same example it would be
Points Lost = 5
I currently have the script for my current Points Needed if it helps, but I really can't figure out the equation for this, luckily not necessary for a school project, just for a solo project so I can learn how to code.
Here is the code for the Points Needed
var pointsNeeded = totalGrade.value - grade.value;
if (gradePercentage > 100) {
pointsNeeded = pointsNeeded * -1;
document.getElementById("pointsNeeded").innerHTML = "You can lose " + pointsNeeded + " point(s) and still have 100%";
}
else {
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
}
else if (gradePercentage < 89, gradePercentage > 79) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.9) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
else if (gradePercentage < 79, gradePercentage > 69) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.8) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
else if (gradePercentage < 69, gradePercentage > 59) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.7) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
else if (gradePercentage < 59) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.6) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
gradePercentage being the actual percentage, totalGrade being the full grade, and grade being the grade you have.
Also these are the grade values,
var gradeLetter = "A"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage > 79.9, gradePercentage < 90) {
var gradeLetter = "B"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage > 69.9, gradePercentage < 80) {
var gradeLetter = "C"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage > 59.9, gradePercentage < 70) {
var gradeLetter = "D"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage < 59.9) {
var gradeLetter = "F"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
This code is pretty self-explanatory:
let startingPoints = 952.3 // How many points you got on the test
let maxPoints = 1049 // Max possible points you could have gotten
// Your grade (0-100%):
let gradePercentage = 100 * startingPoints / maxPoints; // E.g. 90.7816968541468
// How much less you'd need (as a percentage) to get the next lowest grade:
let extraPercentage = ( gradePercentage % 10 ) + 0.01; // E.g. 0.7916968541468015
// Your new grade (as a percentage) if you bumped it down:
let newGrade = gradePercentage - extraPercentage; // E.g. 89.99
// How many points you'd have to reduce your score by to bump it down to the next lowest
let extraPoints = ( extraPercentage / 100 * maxPoints ) // E.g. 8.304899999999947
// What your score would be if you bumped it down to the next level:
let newExtraPoints = startingPoints - extraPoints; // E.g. 943.9951
console.log( `Your grade is ${gradePercentage}% (${startingPoints} points out of a possible ${maxPoints}). If you subtract ${extraPercentage}% (or ${extraPoints} points), your grade will be ${newGrade}% (${newExtraPoints} points)` );
The only trickiness is that we're adding 0.01, which technically could be 0.001, or 0.0001, or...well, we have to add something to drop below the limit, and with no set "right" amount, I'm choosing 0.01 because it works out nicely.
Example output:
Your grade is 90.7816968541468% (952.3 points out of a possible 1049).
If you subtract 0.7916968541468015% (or 8.304899999999947 points),
your grade will be 89.99% (943.9951 points)
I am trying to set a function that creates a random number between a range
I need to make it working with negative values so I can do
randomBetweenRange( 10, 20)
randomBetweenRange(-10, 10)
randomBetweenRange(-20, -10)
This is what I am trying, it is a bit confusing and at the moment randomBetweenRange(-20, -10) is not working..
function randomBetweenRange(a, b){
var neg;
var pos;
if(a < 0){
neg = Math.abs(a) + 1;
pos = (b * 2) - 1;
}else{
neg = -Math.abs(a) + 1;
var pos = b;
}
var includeZero = true;
var result;
do result = Math.ceil(Math.random() * (pos + neg)) - neg;
while (includeZero === false && result === 0);
return result;
}
How can I make it working?
ASSUMING you will always have the little value on first, this code will do the tricks, see the comment below and don't hesitate to ask !
var a=parseInt(prompt("First value"));
var b=parseInt(prompt("Second value"));
var result = 0;
// Here, b - a will get the interval for any pos+neg value.
result = Math.floor(Math.random() * (b - a)) + a;
/* First case is we got two neg value
* We make the little one pos to get the intervale
* Due to this, we use - a to set the start
*/
if(a < 0) {
if(b < 0) {
a = Math.abs(a);
result = Math.floor(Math.random() * (a + b)) - a;
}
/* Second case is we got two neg value
* We make the little one neg to get the intervale
* Due to this, we use - a to set the start
*/
} else {
if(b > 0) {
a = a*-1;
result = Math.floor(Math.random() * (a + b)) - a;
}
}
console.log("A : "+a+" | B : "+b+" | Int : "+(a+b)+"/"+Math.abs((a-b)));
console.log(result);
You have declared the variable 'pos' in the beginning itself. Then why do you declare it in the 'else' part? ( var pos = b;)
Hence, for this statement,
do result = Math.ceil(Math.random() * (pos + neg)) - neg;
'pos' will not have any value.
do result = Math.ceil(Math.random() * (pos + neg)) - neg;
Specifically Math.random() * (pos + neg) returns the wrong range. If pos = -20 and neg = -30, the range between pos and neg should be 10, but your operation returns -50. You should also add one to the range because its technically the amount of possibilities (ex: if you want to generate your function to return {0,1}, the range between pos and neg is 1, but there are two possibilities of numbers to return) and subtract another 1 from result because you're using Math.ceil
Your else clause also redeclares var pos
If you want to generate a number between -50 and 50 - Get a random number between 0 and 100 then subtract 50
var randomNumber = Math.floor(Math.random() * 101) - 50;
console.log(randomNumber);
var current = 12000;
var june = 14600;
var may = 11200;
I want percent change with respect to 'current' month parameter. The output should be in percent and it can add or subtract w.r.t. the current month. How to do this?
Note that if one of your values is 0 you will get either -100% or Infinity%.
This solves that problem:
function percIncrease(a, b) {
let percent;
if(b !== 0) {
if(a !== 0) {
percent = (b - a) / a * 100;
} else {
percent = b * 100;
}
} else {
percent = - a * 100;
}
return Math.floor(percent);
}
Its simple maths:
var res=(current-june)/current*100.0;
var percentchange = (june - current) / current * 100.0;
If your answer is a negative number then this is a percentage increase else decrease.
It isn't an easy task to handle specials cases, increase or decrease, rounding, over 100%, etc.
function calcPc(n1,n2){
return (((n2 - n1) / n1 * 100).toLocaleString('fullwide', {maximumFractionDigits:3}) + "%");
}
console.log(
" May: " , calcPc(11200,12000) ,
"\nJune:" , calcPc(14600,12000)
)
I'm making an "acceleration" array like this:
acc["0100"] = 1;
acc["0300"] = 2;
acc["0600"] = 4;
acc["0900"] = 8;
acc["2000"] = 16;
acc["5000"] = 32;
And, when the user presses a key, I start a timer: this._startTick = (new Date()).getTime();
Now I have a timer that checks if the key is still pressed. If so, then I do something like:
this._delay = (new Date()).getTime() - this._startTick;
And now, based on this._delay, I'd like to find one of the previous values (1, 2, 4 or 8). How would you do that?
NB: if the value is greater than "5.0" then the result should always be 32.
NOTA: my goal is, given an elapsed time, find out which value is the best. I started the way I've just explained, but if you have another solution, I'll take it!
It's easier to operate on an array than on an object:
var accArr = [];
for (time in acc) {
accArr.push({time: time, value: acc[time]});
}
Assuming you have an array, you can do:
function getValue(delay) {
var diffs = accArr.map(function (e) { return Math.abs(e.time - delay); });
return accArr[diffs.indexOf(Math.min.apply(null, diffs))].value;
}
EDIT:
Well, you didn't mention that this is a performance-critical function. In that case, I would recommend picking a granularity (e.g. 0.05, so the multiplier for delay is 20) and pre-calculating all values from 0 to MAX_DELAY:
var multiplier = 20,
granularity = 1 / multiplier;
var delayValues = (function () {
var result = [];
for (var delay = 0; delay <= MAX_DELAY; delay += granularity) {
result.push(getValue(delay));
}
return result;
})();
During the animation, fetching the value will be a simple lookup in a relatively small table:
function getValueFast(delay) {
return (delayValues[Math.round(delay * multiplier)] ||
delayValues[delayValues.length - 1])
}
JSPerf comparison between this solution and simple if statements shows they perform equally fast for searching around a middle value.
Here is the jsfiddle test page.
var getAccForDelay = (function () {
var acc = {
0.1: 1,
0.3: 2,
0.6: 4,
0.9: 8,
2.0: 16,
5.0: 32
};
return function(delay) {
var key,
bestKey = undefined,
absDiff,
absDiffMin = Number.MAX_VALUE;
for (key in acc) {
if (acc.hasOwnProperty(key)) {
absDiff = Math.abs(delay - key);
if (absDiffMin > absDiff) {
absDiffMin = absDiff;
bestKey = key;
}
}
}
return bestKey === undefined ? undefined : acc[bestKey];
};
}());
Test:
console.clear();
console.log(getAccForDelay(0));
console.log(getAccForDelay(0.33));
console.log(getAccForDelay(3.14));
console.log(getAccForDelay(123456.789));
Output:
1
2
16
32
=== UPDATE ===
The above solution doesn't utilize of the fact that acc is sorted by key. I optimized the code by replacing linear search with binary search, which is much faster on long arrays. Here is the test page.
var getAccForDelay = (function () {
var accKey = [ 0.1, 0.3, 0.6, 0.9, 2.0, 5.0 ],
accValue = [ 1, 2, 4, 8, 16, 32 ],
accLength = accKey.length;
return function(delay) {
var iLeft, iMiddle, iRight;
iLeft = 0;
if (delay <= accKey[iLeft])
return accValue[iLeft];
iRight = accLength - 1;
if (accKey[iRight] <= delay)
return accValue[iRight];
while (true) {
if (iRight - iLeft === 1)
return delay - accKey[iLeft] < accKey[iRight] - delay ? accValue[iLeft] : accValue[iRight];
iMiddle = ~~((iLeft + iRight) / 2);
if (delay < accKey[iMiddle])
iRight = iMiddle;
else if (accKey[iMiddle] < delay)
iLeft = iMiddle;
else
return accValue[iMiddle];
}
};
}());
In my humble opinion I think the best solution to this problem is to write a function which picks the best acceleration based on the time using if statements as follows:
function getAcceleration(time) {
if (time < 0.20) return 1;
if (time < 0.45) return 2;
if (time < 0.75) return 4;
if (time < 1.45) return 8;
if (time < 3.50) return 16;
return 32;
}
However this is a static solution. If that's alright with you then I recommend you use this method. On the other hand if you need a dynamic solution then use this instead:
var getAcceleration = createAccelerationMap(0.1, 0.3, 0.6, 0.9, 2.0, 5.0);
function createAccelerationMap(previous) {
var length = arguments.length, limits = [];
for (var i = 1; i < length;) {
var current = arguments[i++];
limits.push((previous + current) / 2);
previous = current;
}
return function (time) {
var length = limits.length, acceleration = 1;
for (var i = 0; i < length;) {
if (time < limits[i++]) return acceleration;
acceleration *= 2;
}
return acceleration;
};
}
Either way you may then use getAcceleration as follows:
console.log(getAcceleration(0)); // 1
console.log(getAcceleration(0.33)); // 2
console.log(getAcceleration(0.64)); // 4
console.log(getAcceleration(1.42)); // 8
console.log(getAcceleration(3.14)); // 16
console.log(getAcceleration(123456.789)); // 32
See the demo: http://jsfiddle.net/QepT7/
If the 0.1 is the number of seconds, and you want to round to 1 decimal you can do something this:
// 0.42332 * 10 = 4.2332
// Math.round( ) will be 4
// 4 / 10 = 0.4
acc[ (Math.round(this._delay * 10) / 10).toString() ]
var seconds = this._delay.toString().substring(0,2)
console.log(acc[seconds]);
This is a straight-forward approach of your problem: First I convert the float to a string, second I cut off everything after the third character.
What units are you using?
this._startTick = (new Date()).getTime();
// ms = ms
this._delay = (new Date()).getTime() - this._startTick;
// ms = ms - ms
So to get to "0.1"/etc from milliseconds I'm assuming you are doing
(Math.floor(ms / 100) / 10).toString();
Why not just keep everything in ms/100 so you can use integers?
var acc = [];
acc[ 1] = 1;
acc[ 3] = 2;
acc[ 6] = 4;
acc[ 9] = 8;
acc[20] = 16;
acc[50] = 32;
Then you can create a "nearest" lookup function like this
function find(x) {
var i = 0;
x = x | 0; // The | 0 will cause a cast to int
if (x < 0) x = 0;
if (acc[x] !== undefined) return acc[x];
if (x > acc.length) return acc[acc.length - 1];
while (++i < acc.length) {
if (acc[x - i] !== undefined) return acc[x - i];
if (acc[x + i] !== undefined) return acc[x + i];
}
}
find(this._delay / 100);
Now examples are
find(30); // 16
find(100.5); // 32
find(0); // 1