0

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.

0

1 Answer 1

2

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); }; 
Sign up to request clarification or add additional context in comments.

1 Comment

Woah, crap. Thank you SO much! The reason there's nothing in the Exotic Bio Disruptor Multiplier is because there was a bug with that too which I was still playing with.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.