ES6 - Create Strings using Template Literals

I find this solution a bit confusing and just hoping for some more insight or clarification.

When defining i < arr.length, Im assuming we do that because we dont want ${arr[i]} to return the entire array. Correct?

If so, then how does ${arr[i]} know to return just one item in the array? And not any number of items in the array as long as it is < arr.length?

I know i++ means to increment. But is it really i++ that is telling us to return just one item at a time, as opposed to any number of items as long as the number of items returned is < arr.length?

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
  const failureItems = [];
  for (let i = 0; i < arr.length; i++) {
    failureItems.push(`<li class="text-warning">${arr[i]}</li>`);
  }
  // Only change code above this line

  return failureItems;
}

const failuresList = makeList(result.failure);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: ES6 - Create Strings using Template Literals

Link to the challenge:

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

when working with for loop , the i represent the index of the item in the array , when you want to acces the value of a specifec item in an array you say array[0] or array[5], we specify i < array.length to say don’t go over that number so you don’t get an error by trying to read a part of the memory that is empty or maybe containe something that you shouldn’t miss with , so it basicly says count from 0 to the length of the array and not more .
so it start at 0 and count item by item until it reaches the end of the array.
and you can do it the other way around by saying i = array.length and count down by i-- until 0 .
hope that answers your question.

1 Like

Yes I think so. But if i < arr.length, then wouldn’t that leave out the last item in the array? How can the last item in an array be included when i has to be < arr.length?

in coding, we count from 0
so the first item in the array is index 0
and the last is index length-1

So when i = 0, the 0 is a reference to array[0], correct?

If there are 3 items in an array, then the last item is array[2]. Since that number 2 being used as syntax is less than the “3” items in the array, then that is why i < arr.length can be valid. Even though array[2] is the 3rd or last item in the array?

Confusing but clarified now. Thank you.

1 Like

yes index 0 is referring to the first item, index 1 the 2nd etc.
(counting always from 0)

try console.log(ayrray.length) on diffrent array , it return a number if the array have one item it returns 1 , but if you try array[1] it return undefined , that’s why it doesn’t leave any item behind , also to understand more try playing around with the numbers and see what you get after each try .

Thanks for all the help guys, I appreciate it!

1 Like