簡體   English   中英

我試圖獲得使用return函數的附加值,但沒有得到| 您能幫助我我到底在做什么錯嗎?

[英]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()); 

如果要將xy值保存到預定義變量,只需將它們作為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM