I was explaining some basic javascript stuff to someone and found myself very embarrassed when a variable was not treated as a number, albeit being a number.
There are no NaN checks here, but we only input numbers.
var number1 = prompt("choose any number");
var number2 = prompt("choose another number");
alert("thank you I will multiply your numbers now!");
var multiplication = function(a, b) {
var c = a * b;
alert("the product of " + number1 + " and " + number2 + " is: " + c);
var number3 = prompt("please choose a number to add to the product.");
var sum = c + number3;
alert(sum);
};
multiplication(number1, number2);
It seems that variable "number3" is not treated as a number and the last alert does not give a sum but rather treats "number3" as a string.
Things get better when I use trivial
var sum = c + +number3;
I cannot understand why number3 is not treated as a number, any ideas? I could not explain this to my friend and had to retreat in disgrace.
Tired on Chrome and Firefox with the same effect.