[英]I am trying to get the addition value of using return function but not getting | Could you help to me what exactly I am doing wrong?
我试图获得使用return函数的附加值。
var x, y; function test(x, y) { x = prompt("Enter a number"); y = prompt("enter another Number"); return (x + y); } test();
.prompt()
返回一个字符串。 您需要使用parseInt()
或parseFloat()
等将其转换为数字:
var x, y, result; function test(x, y) { x = parseInt(prompt("Enter a number")); y = parseInt(prompt("enter another Number")); return (x + y); } console.log(test());
如果要将x
和y
值保存到预定义变量,只需将它们作为test
函数的参数删除-它将使用您的预定义变量:
var x, y, result; function test() { x = parseInt(prompt("Enter a number"), 10); y = parseInt(prompt("enter another Number"), 10); return x + y; } console.log(test(), x, y);
同样按照@ j08691的建议,在求和之前,应先将prompt
结果转换为数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.