1

Can someone help me please fix my code? I was using forEach method in this exercise

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.

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 = [];

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


  // Only change code above this line

  return failureItems;
}

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

it should be resulted to this

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

but in this code my result is this

[ '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>',
  '<li class="text-warning">undefined</li>' ]
5
  • 3
    k refers to the value not the index. So ${k} is enough. Also why not use map instead of forEach? Commented Feb 25, 2022 at 15:27
  • 2
    NB: forEach always returns undefined - not useful to store in a variable result which you don't use either. Commented Feb 25, 2022 at 15:28
  • I was just trying if this exercise can be solve using forEach method. I used ${arr[k]} to access the value of result.failure to print in the console. Commented Feb 25, 2022 at 15:32
  • does it mean I can't use forEach to solve this? Commented Feb 25, 2022 at 15:33
  • 1
    Of course you can, just don't assign its undefined return value to anything since it's of no use anyway... and as mentioned, you need ${k} and not ${arr[k]}. forEach (just like for ... of) will loop over values, not indices. If you wanted indices (which is unnecessarily complicated) you would use .forEach((_, k) => ...) Commented Feb 25, 2022 at 15:34

1 Answer 1

5

You want to concatenate k. Also as mentioned in the comments, Array#forEach returns undefined.

To fix the above:

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

Another solution using Array#map:

function makeList(arr) {
  return arr.map(k => `<li class="text-warning">${k}</li>`);
}

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

const failuresList = makeList(result.failure);

console.log(failuresList);

Sign up to request clarification or add additional context in comments.

3 Comments

yes I saw this solution using map method I was trying if it is possible using forEach
@pichimnc Please look at the answer again. It shows the forEach solution too, just as several people already pointed out in the comments on your quesiton above!
Omg! it works!!! I was wrecking my brain for this yesterday. Thank you for all the help!!!! 😭

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.