Strings using Template Literals assistance

Am I on the right direction? I feel like I am not correct.

  **Your code so far**

const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["no-extra-semi", "no-dup-keys"]
};

function makeList(arr) {
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr === result.failure) {
return result.failure;
}
else {
return [];
}
}

const failureItems = [];
// Only change code above this line

return failureItems;
}

const failuresList = makeList(result.failure);
console.log(failuresList)
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Create Strings using Template Literals

Link to the challenge:

Not quite there.

First of all, be aware that a return will terminate the current function. Here, you are just returning the failure property, so you are returning this (on the first pass):

["no-var", "var-on-top", "linebreak"]

But the description says it wants this:

[
  '<li class="text-warning">no-var</li>',
  '<li class="text-warning">var-on-top</li>',
  '<li class="text-warning">linebreak</li>'
]

Those are not the same thing. You can ignore the spacing and lines and whitespace and whether the strings are delineated with single or double quotes - those don’t matter to JS. You have three elements in your array and they are strings, but the contents of those strings are not what is expected.

if (arr === result.failure) {

First of all, you should not be making any reference to the global variable result inside the function.

Also, we don’t care what arr may match or not. All we care is that they sent us an array - we’re trusting that they are sending us the right array.


So, what do you need here. You need to take the array that is passed to you, and you need to return a new array with each of those elements changed. The simplest way to do that is with a for loop. Create a new array that converts each element in the passed array into what the result wants.

1 Like

First of all , thank you for the answer.

I have another question, may I know why when

  for (let i = 0; i < arr.length; i++) {
  arr.push(i);
  }

It gives me an infinite loop notice?

Because you are adding to arr with each pass of the loop. So, the loop can never reach the end. It’s like you’re doing the dishes and you have to wash 3 plates, but every time you wash a plate, someone puts a new dirty plate on the stack you have to watch. You have to be very, very careful about messing with the length of the array if you you are using the length of the array to control your loop.

For the purposes of this exercise, you should be creating a new array.

1 Like

Shouldn’t it stop tho ? i < arr.length;

another thing const failureItems = []; isn’t that enough as a new array ?

Look closely at your code:

for (let i = 0; i < arr.length; i++) {
  arr.push(i);
}

Let’s say that arr is initially empty, so arr.length is 0 on the first pass through the loop. In this case i is also 0, but 0 is not less than 0 so you will execute arr.push(i). After doing this what is the length of arr? Now you go onto the next pass through the loop when i is 1. Will the loop stop at this point?

1 Like

ah okay, so it becomes 1, when 0 is pushed so it never stops.

It never stops because it keeps adding to the array so you never reach the end.

another thing const failureItems = []; isn’t that enough as a new array ?

Sure, but you’re not using it. maybe that’s where you should be putting your new array elements…