简体   繁体   中英

I am trying to add the sum of three numbers in JavaScript but it's giving me NaN. Why?

I am wanting to collect three numbers from the user and add them using a function and loop. I keep coming up with NaN. Why is this? I've tried converting the string prompt to a Number object to resolve this.

 var userEntry1 = Number(window.prompt("Enter in a number of your choice")); var userEntry2 = Number(window.prompt("Enter in a number of your choice")); var userEntry3 = Number(window.prompt("Enter in a number of your choice")); var sum; var i; function addNumb(userEntry1, userEntry2, userEntry3) { "use strict"; sum = userEntry1 + userEntry2 + userEntry3; for (i = 1; i <= arguments.length; i += 1) { sum += Number(arguments[i]); } return sum; } addNumb(userEntry1, userEntry2, userEntry3); window.console.log(sum); 

In your loop, you're looping equal to the length of arguments , but since it's a 0-based array, you're going one iteration too far. Change the <= to < :

 var userEntry1 = Number(window.prompt("Enter in a number of your choice")); var userEntry2 = Number(window.prompt("Enter in a number of your choice")); var userEntry3 = Number(window.prompt("Enter in a number of your choice")); var sum; var i; function addNumb(userEntry1, userEntry2, userEntry3) { "use strict"; sum = userEntry1 + userEntry2 + userEntry3; for (i = 0; i < arguments.length; i += 1) { sum += Number(arguments[i]); } return sum; } addNumb(userEntry1, userEntry2, userEntry3); window.console.log(sum); 

The arguments object has keys from 0 , so you should loop thru that object from 0 to the keys.length - 1 .

The object arguments is actually a key-value object, so you can use the operator in .

 var userEntry1 = Number(window.prompt("Enter in a number of your choice")), userEntry2 = Number(window.prompt("Enter in a number of your choice")), userEntry3 = Number(window.prompt("Enter in a number of your choice")), sum, i; function addNumb(userEntry1, userEntry2, userEntry3) { console.log(arguments)// keys from zero to keys.length - 1 "use strict"; sum = userEntry1 + userEntry2 + userEntry3; for (var arg in arguments) sum += Number(arguments[arg]); return sum; } addNumb(userEntry1, userEntry2, userEntry3); console.log(sum); 

You're already summing the three entered numbers ( sum = userEntry1 + userEntry2 + userEntry3; ) and don't need the for loop at all. If you have to use a for loop, then you should delete the sum = userEntry1 + userEntry2 + userEntry3; line. With "use strict" you will then have to remove the function argument declarations to avoid "variable is declared but not used" errors.

 var userEntry1 = Number(window.prompt("Enter in a number of your choice")); var userEntry2 = Number(window.prompt("Enter in a number of your choice")); var userEntry3 = Number(window.prompt("Enter in a number of your choice")); function addNumb(userEntry1, userEntry2, userEntry3) { "use strict"; var sum = userEntry1 + userEntry2 + userEntry3; return sum; } function addNumb2() { "use strict"; for (var i = 0, sum = 0; i < arguments.length; i++) { sum += Number(arguments[i]); } return sum; } console.log(addNumb(userEntry1, userEntry2, userEntry3)); console.log(addNumb2(userEntry1, userEntry2, userEntry3)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM