1

I want my for loop to print out every item in the array, not just the last item. Cant figure out where I'm going wrong:

var patients = ["Julia", "Kelly", "Thomas", "Clare"];

function lineOfPatients(line) {
 if (!line.length) {
     return "Empty"
 }
 for(var i = 0; i < line.length; i++) {
     var list = `${i + 1}. ${line[i]},`
 }
  return `The line is currently: ${list}`
}

 lineOfPatients(patients)

This returns "The line is currently: 4. Clare,"

I want it to return "The line is currently: 1. Julia, 2. Kelly, 3. Thomas, 4. Clare"

1
  • 3
    Initialize list to "" and then use list += ... not list = ... Commented Apr 12, 2017 at 19:38

5 Answers 5

2

You can call join method on lines array which will contain your lines.

var patients = ["Julia", "Kelly", "Thomas", "Clare"];

function lineOfPatients(line) {
    if (!line.length) {
        return "Empty";
    }

    var lines = [];

    for(var i = 0; i < line.length; i++) {
        var list = `${i + 1}. ${line[i]}`
        lines.push(list)
    }

    return `The line is currently: ${lines.join(", ")}`
}

console.log(lineOfPatients(patients))

Sign up to request clarification or add additional context in comments.

3 Comments

This is the best solution, IMO
@mhodges, Thanks mate!
Best of both worlds, nice.
2

Your issue is that you are reassigning the list variable each time through the loop, so you are overwriting the previous value.

To avoid this, use the += operator instead of the = operator like so:

var patients = ["Julia", "Kelly", "Thomas", "Clare"];

function lineOfPatients(line) {
 if (!line.length) {
     return "Empty"
 }
 var list = "";
 for(var i = 0; i < line.length; i++) {
     list += `${i + 1}. ${line[i]}, `
 }
  return `The line is currently: ${list}`
}

console.log(lineOfPatients(patients))

Comments

1

The problem with your code is that with each iteration of your for-loop, you redeclare var list = `${i + 1}. ${line[i]},` so that by the time you return, list only equals the last element in the array.

You could do this:

function lineOfPatients(line) {
  if (!line.length) {
    return "Empty"
  }
  var returnString = "The line is currently: "
  for(let i = 0; i < line.length; i++) {
    let patient = ` ${i + 1}. ${line[i]},`;
    returnString += patient;
  }
  return returnString;
}

Comments

1

Here's what you want. (Got carried away with code golf)

const patients = ["Julia", "Kelly", "Thomas", "Clare"]

const lineOfPatients = (line) => "The line is currently: " + (!line || !line.length) ? "Empty" : line.map((patient, idx) => `${idx + 1}. ${patient}`).join(', ')

console.log(lineOfPatients(patients))

The reason it's not working is because your redeclaring the variable liston every loop. Even if you moved it out of the loop, your not appending the output from the for-loop, your assigning it. It will always be the output from the last loop.

Comments

0

var list is being declared inside your loop. This means that it is being recreated with a new value on each itteration. Declare this variable outside of the loop, as an array. E.g,

var list = new Array();

Then, in your loop, add to the array;

list[i] = .....

3 Comments

string concatenation will suffice here
True. I guess it depends on what they wants to do with the items. If it's just print, then yes. Otherwise it's helpful to separate them.
Agreed - should be new Array() or [] though

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.