Prompt user to Input Numbers in Array and then Sum it using JavaScript
问题 This works fine if I sum numbers in JavaScript in an Array. But when I ask their input from User then they are printed as if the numbers are in string. Kindly help me in finding the flaw in my code. var tArr = []; for(var f = 1;f<=4;f++) { // tArr.push(f); var z = prompt("Enter numbers for Sum"); tArr.push(z); } var r = parseInt(tArr); alert(tArr); var summ = 0; for(var w = 0; w< tArr.length; w++) { summ += tArr[w]; } console.log(summ); 回答1: To convert all values to number just do +tArr[w]