var apples = prompt('Please enter no. of apples');
var oranges = prompt('Please enter no. of oranges');
var fruits = apples + oranges;
document.write(fruits);
Why does it work with - and * and not +?
Thanks!
var apples = prompt('Please enter no. of apples');
var oranges = prompt('Please enter no. of oranges');
var fruits = apples + oranges;
document.write(fruits);
Why does it work with - and * and not +?
Thanks!
You're adding two strings together to get another string. That's how JavaScript does it.
Maybe what you want is numbers:
var fruits = parseInt(apples, 10) + parseInt(oranges, 10);
As a note, using prompt
to collect information is utterly barbaric. What you need to do is have two input boxes and a submit trigger that does the math, or since it's so trivial, hook it up to trigger on any change to either value. jQuery basics here.
You're doing string concatenation with the + instead of an addition. Parse to float or int.
var fruits = parseFloat(apples) + parseFloat(oranages);
prompt returns a string, thus it's appending the strings together. Javascript will convert to an int when you try other operators on the variables.
Use parseInt to make sure they're ints.