-3

There's a lot of repetitive code, so I won't include it all, but the main jist of it is here:

var Qs1; 
var Qs2;
var Qs3;
var Qs4;
var Qs5;
var Qs6;
var Qs7;
var Qs8;
var Qs9;
var Qs10;

if(boilingPan25 == 0) {
    var Qs1 = nBP * QBP * Zi0;
} else if(boilingPan25 == 1) {
    var Qs1 = nBP * QBP * Zi1;
}   else if(boilingPan25 == 2) {
    var Qs1 = nBP * QBP * Zi2;
} else if(boilingPan25 == 3) {
    var Qs1 = nBP * QBP * Zi3;
}   else if(boilingPan25 == 4) {
    var Qs1 = nBP * QBP * Zi4;
} else {
    var Qs1 = 5 * QBP * Zi5;
}

//document.write(Qs1);

if(tiltingPan70 == 0) {
    var Qs2 = nTP * QTP * Zi0;
} else if(tiltingPan70 == 1) {
    var Qs2 = nTP * QTP * Zi1;
}   else if(tiltingPan70 == 2) {
    var Qs2 = nTP * QTP * Zi2;
} else if(tiltingPan70 == 3) {
    var Qs2 = nTP * QTP * Zi3;
}   else if(tiltingPan70 == 4) {
    var Qs2 = nTP * QTP * Zi4;
} else {
    var Qs2 = 5 * QTP * Zi5;
}

So the values of the variables Qs1 ... Qs10 are found through the if loops. When I go to add all of these values together, it prints out a concatenation of the numbers, rather than simply adding them. Here's how I'm printing it out:

document.write(Qs1 + Qs2 + Qs3 + Qs4 + Qs5 + Qs6 + Qs7 + Qs8 + Qs9 + Qs10);

I've tried adding ".value" to the ends of each variable, but that doesn't work. Any suggestions? Please with Javascript only, no jQuery. Thanks.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Coder
  • 55
  • 7

2 Answers2

1

So I'll go out on a limb here and say you're getting the values either

  • From an input (in which case this is a duplicate of this question), or

  • From prompt (in which case it's a duplicate of this question).

Either way, the values are strings. If you want to use them as numbers, parse them with parseInt, parseFloat, Number, or the unary +:

var num = parseInt(str);
// or
var num = parseFloat(str);
// or
var num = Number(str);
// or
var num = +str;

The last two do the same thing; the first two do slightly different things (from each other and from the last two). See the documentation of them for details.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

Use parseInt(), parseFloat(), Number() etc...

var myString = '1';
window.alert(myString + myString); // output = 11

var myNumber = parseInt(myString);
window.alert(myNumber + myNumber); // output = 2
DeclanPossnett
  • 376
  • 1
  • 5
  • 13