GreenFerret95
GreenFerret95

Reputation: 117

Adding numbers in an Array. Getting weird output. Javascript

    var array = [];        
    getNum();
    document.write(array);
    document.write("<br>Sum: " + sum(array));

    //Function to Sum Array
    function sum(params){
        var total = 0 ;
        for (i = 0; i < params.length; i++){
            total += params[i];
        }
        return total;
    }


    //Function to get Numbers in Array
    function getNum(){
        var count = 0;
        alert("Please enter 5 numbers");
        while(count < 5) {
            array[count] = prompt("Number " + (count + 1));
            count++;
        }
    }

Current Output

5,4,3,2,1
Sum: 054321

What I want

5,4,3,2,1
Sum: 15

I'm trying to make a program where the user adds numbers to an Array and the program calculates different things about those numbers.

When I call sum(array); with a preset array such as var array = [5,4,3,2,1]; The summing works fine and outputs 15 as expected.
However instead of having a preset array, when I include the function to get the set of numbers for the Array, the summation the output is 054321.

I want to do the array calculations manually for my own understandings sake, rather than using reduce();

What am I doing wrong?

Upvotes: 0

Views: 520

Answers (6)

visigoth
visigoth

Reputation: 208

Do everything the same, just parse the user input to an integer. The issue here is that the numbers are being treated as strings.

Modify your sum function like this:

//Function to Sum Array
function sum(params){
    var total = 0 ;
    for (i = 0; i < params.length; i++){
    $num = parseInt(params[i]);
    total += $num;
    }
    return total;
}

That's it, you're done.

Upvotes: 0

bowheart
bowheart

Reputation: 4676

Here's a one-line solution:

function sum(params) {
    return params.map(function(item) { return parseInt(item); }).sum()
}

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 78006

Parse your prompt value as an integer - it's being added as a string and concatenated with the + sign:

array[count] = parseInt(prompt("Number " + (count + 1)));

http://jsfiddle.net/4kp825tc/

MDN also recommends to cast as a Number as another option:

Please note that result is a string. That means you should sometimes cast the value given by the user. For example, if his answer should be a Number, you should cast the value to Number. var aNumber = Number(window.prompt("Type a number", ""));

Upvotes: 4

Will
Will

Reputation: 1299

Your inputs are stored as strings in your array. You'll need to convert the value to a number for the summation to occur correctly.

Use parseInt (set to base 10) to get the correct answer.

        total += parseInt(params[i],10);

http://jsfiddle.net/biz79/x9cbm7Lj/

Upvotes: 1

Igor
Igor

Reputation: 1

The values from prompt are strings and thats why the don't add as you wish add plus before params[i] and your have it correct

var array = [];        
getNum();
document.write(array);
document.write("<br>Sum: " + sum(array));

//Function to Sum Array
function sum(params){
    var total = 0 ;
    for (i = 0; i < params.length; i++){
        total += +params[i]; //make the params[i] to be a number
    }
    return total;
}


//Function to get Numbers in Array
function getNum(){
    var count = 0;
    alert("Please enter 5 numbers");
    while(count < 5) {
        array[count] = prompt("Number " + (count + 1));
        count++;
    }
}

Upvotes: 0

Salih Erikci
Salih Erikci

Reputation: 5087

   //Function to Sum Array
    function sum(params){
        var total = 0 ;
        for (i = 0; i < params.length; i++){
            var temp = parseInt(params[i]);
            total += temp;
        }
        return total;
    }

Upvotes: 1

Related Questions