Covert
Covert

Reputation: 491

Why does my javascript function returns only first item of an array ?

I am a newbie to Javascript and trying my hands on it.

I wrote 2 functions.

The 1st function returns an array of digits and 2nd one loops through it and sums up the array but instead of that, it returns me the first item of an array only. Why ?

function addWorth()
    { 


        var table1= document.getElementById("tableNetWorths");

        var rowCount1= table1.rows.length;

        //var row1= table1.insertRow(rowCount1);


        var arr= [];


       for(var count = 0; count < rowCount1; count++)
       {    
            arr.push(table1.rows[count].cells[1].innerHTML);          
       }


       arr.shift();
       return arr;

    } 

    function showWorthSum()
    {
        var returnedArr= [];

        returnedArr.push(addWorth());

         totalWorth= 0;

        var arrCount= returnedArr.length;

        for(var count = 0; count < arrCount; count++)
        {    
             totalWorth= parseInt(totalWorth)+ parseInt(returnedArr[count]); 

        }

        return parseInt(totalWorth);
    }

button:

 <button class="btn btn-primary" onclick="document.write(showWorthSum())" type="button">Show Sum</button>

array:

100,200,344,22,122,99

Upvotes: 0

Views: 910

Answers (1)

MaxZoom
MaxZoom

Reputation: 7753

This should fix the issue:

var returnedArr = addWorth();

Upvotes: 3

Related Questions