Update: The solution to this query is provided below, courtesy of dougtesting from a different discussion. add array together, display sum
function hello() {
var arr = [];
for (var i = 0; i < 10; i++) {
arr.push(prompt('Enter number' + (i+1)));
}
var total = 0;
for(i=0; i<arr.length; i++) {
var number = parseInt(arr[i], 10);
total += number;
}
console.log(total);
}
//End of response.
I am attempting to allow a user to input 10 numbers, then add those numbers together and show the result to the user. I managed to store the number of inputs (10) in an array, but I am unable to access the contents of the arrays. It seems like I might be overlooking something simple. Would you kindly review and assist me with this issue?
// https://stackoverflow.com/questions/28252888/javascript-how-to-save-prompt-input-into-array
var arr = []; // initializing our array
for (var i = 0; i < 10; i++) { // loop 10 times
arr.push(prompt('Enter number' + (i+1))); // inserting values into the array
}
alert('Full array: ' + arr.join(', ')); // displaying the result
var arrEquals = []; //Empty Arr
arrEquals = arr.push(); //converting string into variable
alert (arrEquals);//displaying string for debugging
//(for loop) printing out # of elements in the array. unfortunately does not output the array content
//this is only half of the solution
for (var a = 0; a < arrEquals; a++){
var a = Number(a); //ensuring input as Number()
console.log(a + "A"); //used for troubleshooting
}
//computing sums in array and adding them together
//the other half of the problem
// https://www.w3schools.com/jsref/jsref_forEach.asp
// var sum = 0;
// var numbers = [65, 44, 12, 4];
// function myFunction(item) {
// sum += item;
// demo.innerHTML = sum;
// }