+ 1

Why doesn't this function return the sum?

function addit(x,y) { var c = (x + y); document.write("the sum is "+ c); } var x = prompt ("Type in first number"); var y = prompt ("Type in second number"); addit (x,y);

3rd Feb 2017, 1:10 PM
Kaj Te Briga
Kaj Te Briga - avatar
3 Answers
+ 1
Because to return a value, the function must have a 'return' statement ^^
3rd Feb 2017, 1:17 PM
visph
visph - avatar
+ 1
OK, I get it, thank you everyone for answering :-)
3rd Feb 2017, 1:20 PM
Kaj Te Briga
Kaj Te Briga - avatar
+ 1
I guess javascript (as python) accepts standard input as string, not integer so you have to convert it, something like this. Srećno :) function addit(x,y) { var c = (Number(x) + Number(y)); return c; } var x = prompt ("Type in first number"); var y = prompt ("Type in second number"); var c = addit (x,y) document.write("the sum is "+ c);
3rd Feb 2017, 1:24 PM
Ladislav Milunović
Ladislav Milunović - avatar