Skip to content Skip to sidebar Skip to footer

Add Array Together, Display Sum

What i am trying to do is take the user input which is numbers then add those numbers together and display the sum. I have an array, I have the sum of the array. Now I need to dis

Solution 1:

Well if you want just the total output at the end then you need to log total to the console after the loop. Based on my understanding of what you want to do I think something like the following is all that's needed...

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);
}

Post a Comment for "Add Array Together, Display Sum"