Function forloop

Guys in my school are really busy,and they are asking to complete this exercise:

Build a function forLoop. It takes an array as an argument. Start counting from 0, and, using a for loop, add a string to the array 25 times. But not just any string. If your i value is 1, add the string “I am 1 strange loop.”; if your i value is anything else, add the string “I am ${i} strange loops.”. (Remember flow control with if and else? And how do we interpolate i?) Then return the array.

my code is this one:

function forLoop (array){
for (let i = 0; i < 25; i++) {
if (i=== 0 ){
console.log("I am 1 strange loop. ");

} else {
console.log("I am ${i} strange loops. " );

}
return array;
}

}

I saw the first mistake,and it’s in the If statement i must be equal to 1,not to 0

"I am ${i} strange loops. " should be

`I am ${i} strange loops. `

“” you have to concat -> “I am”+ i + “strange loops.”
With ``, in ES6, make a string with a var is much easier, you can do

`I am ${i} strange loops. `

, so you only have to use ${} and set a var/const.

1 Like

Thank you for this :blush:

guys I can’t do it with my keyboard,would you mind to please correct it for me?

function forLoop(array){
  for (let i = 0; i < 25; i++)
  {
    if (i === 1) {
    array.push( ``I am 1 strange loop. ``);
    }
    else {
    array.push( ``I am ${i} strange loops.``);
    }
  }
  return array;
}

i dontk now how i configure the mac the first time,that I have even the @ on alt + g

function forLoop(array){
for (let i = 0; i < 25; i++)
{
if (i === 1) {
array.push(I am 1 strange loop.);
}
else {
array.push(I am ${i} strange loops.);
}
}
return array;
}

50

the online teacher from the school had me 2 hours,for at the end guys,you told me it was just that…

I am so grateful,I could not do it without you guys

forLoop