0

I am new to arrays, and I have been trouble with making a for loop work with an array. What do I need to do?

function start() {
    var arrPeople = ['Adele', 'Ted', 'Jo', 'Jim', 'Emma', 'Kate', 'Christopher'];
    var text = '';

    for (i = 0; 1 < arrPeople.length; i++) {
        text += (arrPeople[i] + '<br />');
    }
}
3
  • please give what exactly do you want? Commented Apr 14, 2017 at 5:18
  • try changing the 1 to i Commented Apr 14, 2017 at 5:19
  • 1
    if you want to avoid the for loop and only want to join the array element with <br/> tag you can also use this text= arrPeople.toString().replace(new RegExp(',', 'g'), '<br/>'). Commented Apr 14, 2017 at 5:27

3 Answers 3

3

There were a couple of minor errors in your function. The for loop statement is three parts: variable initialization, a condition that's checked after each loop, and an action that's performed after each loop. You had mistyped your condition as 1 < arrPeople.length which would always be true. Also you weren't returning a value from the function to be used by the calling code.

function start() {
    var arrPeople = ['Adele', 'Ted', 'Jo', 'Jim', 'Emma', 'Kate', 'Christopher'];
    var text = '';

    for (var i = 0; i < arrPeople.length; i++) {
        text += (arrPeople[i] + '<br />');
    }
    return text;
}
Sign up to request clarification or add additional context in comments.

Comments

1

pick your poison ...

you can also use array.foreach ...

function start() {
    var arrPeople = ['Adele', 'Ted', 'Jo', 'Jim', 'Emma', 'Kate', 'Christopher'];
    var text = '';

    arrPeople.forEach(function(person) { 
        text += person + '<br />';
    });
}

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

here's an possibly an even simpler solution using array.join ...

function start() {
    var arrPeople = ['Adele', 'Ted', 'Jo', 'Jim', 'Emma', 'Kate', 'Christopher'];
    var text = arrPeople.join("<br />");

    text += "<br />";
}

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

or if you prefer, you can also use the functional style using array.reduce ...

function start() {
    var arrPeople = ['Adele', 'Ted', 'Jo', 'Jim', 'Emma', 'Kate', 'Christopher'];

    var text = arrPeople.reduce(function(txt, itm) { 
        return txt + '<br />' + itm;
    })

    text += "<br />";
}

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

1 Comment

Some good options there, but you repeated the OP's mistake of not returning anything from your functions!
0

Looks like you just want to display the list of names in the array vertically. You can do it without for-loop.

function start() {
    var text = [ 'Adele', 'Ted', 'Jo', 'Jim', 'Emma', 'Kate', 'Christopher' ].join( '<br />' );
}

You can use join to concatenate each array item and you can specify a separator for each. This way there is no extra <br/>. see array.join

1 Comment

Good thought, but you repeated the OP's mistake of not returning anything from the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.