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
I made a slight edit to your post, to reflect the single backticks.
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;
}
Can you upload a photo of your keyboard? Most of the time the backtick is located above the Tab key or to the left of the 1 key.
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;
}
How are you typing those backticks above? You said you did not know how to find a backtick on your keyboard.
Anyway, a template literal will start with a single backtick ` and end with a single backtick. You are using two on the front and two on the end.
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
So did you create a final solution which meets your requirements?