0

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

6
  • 2
    each assignment results = accountsArray[i]; overwrites previous one Commented Oct 12, 2014 at 18:26
  • addAccount(), which populates the array, is never called, at least from the code you've posted. Commented Oct 12, 2014 at 18:27
  • @Utkanos It is called in the HTML 'onclick' Commented Oct 12, 2014 at 18:28
  • Where and when is accountsListHtml() called ? If it's before addAccount() result must be empty. Commented Oct 12, 2014 at 18:40
  • @MartinErnst Take a look at all the code Balance Pro Commented Oct 12, 2014 at 18:44

4 Answers 4

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

4 Comments

Still getting undefined.
Can you make a jsfiddle.net example?
It's a little complicated for jsfiddle. Here's a link to my server where I uploaded the code. It is an iOS web app using Framework7. link
Don't really know what you expect - it's working fine (at least the way I see it) on Chrome 37. See this screenshot.
0

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.

1 Comment

Hmm, just tried that and now it's just returning the empty string (results = ' ')
0

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 = []

2 Comments

I tried this on my end, and I'm still not sure why it isn't working. I just uploaded your edits to the code, Balance Pro
@jryankennedy I think the issue is probably related to the order in which your functions are being called, in that case. It looks like accountsListHtml is invoked once, when the <body> loads and the form fields are empty (yielding zero results). Either way, it looks like it wouldn't be an issue with looping through an array at this point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.