0

Just started JS.

I have a button and it calls a function which should loop through an array and show me each item but it doesn't. It says that:

showWorthSum() function is not defined.

   function addWorth()
    { 


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

        var rowCount1= table1.rows.length;

        var row1= table1.insertRow(rowCount1);


        var arr= [];


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


       arr.shift();
       return arr;

    } 

    function showWorthSum()
    {
        var returnedArr= [];

        returnedArr.push(addWorth());

        var totalWorth= 0;

        var arrCount= 10 ;

        for(int count = 0; count < arrCount; count++)
        {    
             //totalWorth= totalWorth+ returnedArr[count]; 

            document.write(returnedArr[count]);
            //debugger;
        }

        //return totalWorth;
    }

Button:

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

And yes I have taken care of script tags etc, just stripped those for the purpose of posting.

Update: (Full code)

<!DOCTYPE html>
<html>
<head>
<link href="css/basic.css" rel="stylesheet">
<link href="css/bootstrap.min.css" rel="stylesheet">

<script>
    function alterTable()
    { 

        //Textboxes code

        var tBoxCompany= document.getElementById("txtboxCompany");
        var tBoxAmount= document.getElementById("txtboxAmount");



        //table code

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

        var rowCount= table.rows.length;

        var row= table.insertRow(rowCount);

        var Cell1= row.insertCell(0);
        var Cell2= row.insertCell(1);

        Cell1.innerHTML= tBoxCompany.value;
        Cell2.innerHTML= tBoxAmount.value;

    }    

    function addWorth()
    { 


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

        var rowCount1= table1.rows.length;

        var row1= table1.insertRow(rowCount1);


        var arr= [];


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


       arr.shift();
       return arr;

    } 

    function showWorthSum()
    {
        var returnedArr= [];

        returnedArr.push(addWorth());

        var totalWorth= 0;

        var arrCount= 10 ;

        for(int count = 0; count < arrCount; count++)
        {    
             //totalWorth= totalWorth+ returnedArr[count]; 

            document.write(returnedArr[count]);
            //debugger;
        }

        //return totalWorth;
    }

</script>

</head>
<body>
<div class="container-fluid">
<div class="row">
 <div class="col-md-12">

<table id="tableNetWorths">
<tr>
<th>Company</th>
<th>Net Worth ($)</th>

</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>100</td>

</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>200</td>

</tr>
<tr>
<td>Ernst Handel</td>
<td>344</td>

</tr>
<tr>
<td>Island Trading</td>
<td>22</td>

</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>122</td>

</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>99</td>

</tr>
</table>

</div>
</div>

<div class="row">
   <br>
   <div class="col-md-3">
       <input type="text" id="txtboxCompany" class="form-control"/>
   </div>
   <div class="col-md-3">
       <input type="text" id="txtboxAmount" class="form-control"/>
   </div>
   <div class="col-md-3">        
        <button class="btn btn-primary" onclick="alterTable()" type="button">Add Rows</button>
   </div>
    <div class="col-md-3">        
        <button class="btn btn-primary" onclick="document.write(showWorthSum())" type="button">Show Sum</button>
   </div>
</div>



</div>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>

Update: (ShowWorthSum function modified, still not working)

 function showWorthSum()
    {
        var returnedArr= [];

        returnedArr.push(addWorth());

        var totalWorth= 0;

        var arrCount= 10 ;

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

        }

        return totalWorth;
    }
Covert
  • 480
  • 1
  • 7
  • 25

2 Answers2

2

The problem is that you re not defining your variable count. Use this :

for(var count = 0; count < arrCount; count++){ ...

And with JS, no matter if your var is a string or an int, you allways declare your variable using var

O. Paquay
  • 276
  • 1
  • 10
1

remove 'int' from your for loop definition. You have:

for(int count = 0; count < arrCount; count++)

you need:

for(count = 0; count < arrCount; count++)

Also, your loop will return a series of 'undefined' results as it iterates past the number of elements in your actual array. You have six elements, you're looping a fixed 10 times. So once you get past the 6th element, all the rest return (correctly) as undefined.

Bob Dill
  • 1,000
  • 5
  • 13