jryankennedy
jryankennedy

Reputation: 111

Javascript: loop through array

This is driving me crazy. I'm just trying to print out an array and it's not working. What am I missing? The results variable is returning "undefined" which much mean my for loop isn't working correctly. Everything else works properly, the console.log I have correctly displays the fields are added to the array.

// The list of accounts array.
var accountsArray = [];

function addAccount() {
  // Take fields and put user data into varables.
  var accountName = document.getElementById('accountName').value;
  var accountBalance = document.getElementById('accountBalance').value;
  var accountType = document.getElementById("accountType");
  var accountTypeSelected = accountType.options[accountType.selectedIndex].text;
  var accountCurrency = document.getElementById("accountCurrency");
  var accountCurrencySelected = accountCurrency.options[accountCurrency.selectedIndex].text;

  // Put these variables into the array.
  accountsArray.push(accountName);
  accountsArray.push(accountBalance);
  accountsArray.push(accountTypeSelected);
  accountsArray.push(accountCurrencySelected);

  // Items added to the array, logged.
  console.log('user added: ' + accountsArray);
}

function accountsListHtml() {

  var results;

  // Loop through the array
  for (var i = 0; i < accountsArray.length; i++) {
    results = accountsArray[i];
  }

  document.getElementById('accountsList').innerHTML = results;
}

Here's a link to all the files. It's an iOS web app using Framework7. Balance Pro

Upvotes: 0

Views: 124

Answers (4)

Martin Ernst
Martin Ernst

Reputation: 3269

You are calling accountsListHtml() in body.onload. At that point accountsArray is empty. I can't find any other possibility to call accountsListHtml() on that page you linked to.

Add one line inside function addAccount() and it will work:

function addAccount() {

    /* vour code */

    console.log('user added: ' + accountsArray);

    accountsListHtml(); // add this line
}

Upvotes: 2

alex
alex

Reputation: 7433

The following works for me: http://jsfiddle.net/95ztrmk3/13/

HTML:

<div id="accountsList"></div>

JS:

// The list of accounts array.
var accountsArray = [];

addAccount();
accountsListHtml();

function addAccount() {
    // Take fields and put user data into varables.
    var accountName = "John Doe";
    var accountBalance = "500.00";
    var accountTypeSelected = "Checking"
    var accountCurrencySelected = "USD";

    // Put these variables into the array.
    accountsArray.push(accountName);
    accountsArray.push(accountBalance);
    accountsArray.push(accountTypeSelected);
    accountsArray.push(accountCurrencySelected);

    // Items added to the array, logged.
    console.log('user added: ' + accountsArray);
}

function accountsListHtml() {

    var results = [];

    // Loop through the array
    for (var i = 0; i < accountsArray.length; i++) {
        results += accountsArray[i] + " ";
    }

    document.getElementById('accountsList').innerHTML = results;
    console.log(results);

}

Assuming the input isn't malformed or otherwise weird. I made sure Javascript recognizes results is an empty array and not a string or something: var results = []

Upvotes: 0

myusuf
myusuf

Reputation: 12230

for (var i = 0; i < accountsArray.length; i++) { results = accountsArray[i]; }

The statement in the for loop i.e. results = accountsArray[i]; overwrites the variable results evry loop run. You could change the statement to :

results += accountsArray[i].toString();

and initialise results to an empty string.

Upvotes: 0

Michal Leszczyk
Michal Leszczyk

Reputation: 1889

Try changing results = accountsArray[i]; to results += accountsArray[i];.

Update And initialize results with an empty string, for example :)

Upvotes: 0

Related Questions