Template Literals using forEach()

Tell us what’s happening:
Describe your issue in detail here.

The solution that I did is giving me this result

[ 'no-var',
  'var-on-top',
  'linebreak',

  'no-var',
  'var-on-top',
  'linebreak',

  'no-var',
  'var-on-top',
  'linebreak' ]

when I used the forEach method inside the for in loop. It was incrementing all the values of failure to every original value of failure. Basically, it is the value of

("no-var", )
'no-var',
  'var-on-top',
  'linebreak',

("var-on-top")
  'no-var',
  'var-on-top',
  'linebreak',

( "linebreak")
  'no-var',
  'var-on-top',
  'linebreak' ]

I’ve seen the other solutions but is it possible to use forEach() method in this exercise?

  **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
  const failureItems = [];
  
  for (let i in arr) {
arr.forEach(k => failureItems.push(k));
  }
  return failureItems;
}


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

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36

Challenge: Create Strings using Template Literals

Link to the challenge:

Yes, no issue at all, but I’m not sure why you’re looping twice. The function takes an array of strings, look:

const failuresList = makeList(result.failures)

result.failures is an array of strings, not an array of arrays or an object of arrays.

Plus you aren’t doing the key thing being asked here (using template literals)

Oh yes, because I thought if I put the forEach inside the for in will give me the same result since if I do it directly in

const failureItems = arr.forEach(k => `<li class="text-warning">${k}</li>`); 

//I also tried this

  const failureItems = arr.forEach(k => `<li class="text-warning">${arr[k]}</li>`);


It’s giving me both undefined results. What am I missing to access the string of arrays?

forEach doesn’t return anything, it’s for side effects. It’s the same as a for loop, at the minute you’re doing exactly the same as

for (let k = 0; k < arr.length; k++) {
  `<li class="text-warning">${arr[k]}</li>`;
}

You would still need to push to the given empty array

Edit: sorry, didn’t read your code closely enough, as @JeremyLT picked up on, what you’ve written is same as this (before you added the push):

for (let k of arr) {
  `<li class="text-warning">${arr[k]}</li>`;
}

Or

for (let k = 0; k < arr.length; k++) {
  `<li class="text-warning">${arr[arr[k]]}</li>`;
}

I think I’m close but it’s still giving me undefined, it’s already inside the result.failure because I checked by adding another string/num.

I know that I need to make another const so I put the forEach in const hello but where should I return or call it? I think it’s the reason why it’s giving me “undefined” when accessing the values of failure?

const failureItems = [];
const hello = arr.forEach(k => failureItems.push(`<li class="text-warning">${arr[k]}</li>`));

//this is the result
[ '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>' ]

Why are you doing this hello = business?

Also, if you look at the example on MDN, forEach() gives you elements, not indices.

I just want to share that I got help in stackoverflow site to solve this and I want to share the final code using forEach() method.

Thank you for everyone who helped me! :slight_smile:

Here’s the code

  const failureItems = [];

   arr.forEach(k => failureItems.push(`<li class="text-warning">${k}</li>`));

  // Only change code above this line

  return failureItems;
}

const failuresList = makeList(result.failure);
console.log(failuresList);

This is still wrong. I was trying to point you above to the fact thot the return value of forEach is undefined so this piece does nothing for you.

It looks like there is lots of good advice in that StackOverflow thread but you seem to have only paid attention to the part where someone wrote the correct contents of the forEach for you. I recommend reading through and actually using all of their advice instead of just getting them to write the answers for you. They have a lot of great info they wrote out for you.

1 Like

Yes sorry, I reread the comments and now I understand why it’s giving me undefined when I assigned it to result since it’s not needed to be assigned to it. I revised the code already. Thank you

1 Like

Cool. It’s always exciting getting code to run correctly, but for one off code in challenges like this, the value is in the journey, or something like that fm motivational posters and whatnot.