Switch Case in a forEach function? Code not taking effect

I will start by saying that this is my attempt at the " Intermediate Algorithm Scripting: Convert HTML Entities" problem, so spoilers for anyone who hasn’t yet completed it.

I’m essentially trying to use a switch case to handle each of the entities individually, which seems like the easiest thing to do should I have to ever add more. I want to use this switch case inside a forEach function (or any function that works on an array for that matter; forEach just seemed like the most appropriate), because it’s easier to read than writing out a for statement/nested function in my opinion. However, the switch doesn’t seem to be having any effect on convertArr.

The code I’ve pasted below uses “target = result”, but I’ve also tried splice and replace; none of them seem to work. This lead me to believe that a switch case can’t be used inside a forEach statement, but nothing I’ve found online seems to suggest that’s the case.

function convertHTML(str) {
  let convertArr = str.split("");
  convertArr.forEach(target => {
    switch (target) {
      case "&":
      target = "&";
      break;
      case "<":
      target = "&lt;";
      break;
      case ">":
      target = "&gt;";
      break;
      case "\"":
      target = "&quot;";
      break;
      case "\'":
      target = "&apos;";
      break;
    }
  });
  return convertArr.join("");
}

console.log(convertHTML("Dolce & Gabbana"));

Hello @AndrewGarrison, welcome the the forum.

To answer your question, your forEach and switch statement are doing nothing in regards of updating a string.

In a forEach loop is up to you and your callback to decide what to do on each iteration, are you sure you are not thinking of some other method that return a value like array.map or array.reduce?

Given that in a forEach loop you are responsible for the outcome of the callback, your switch statement is not really returning anything besides assigning a new value to target.

What to do with that target is probably what you need to figure out
Hope this will help :+1:

1 Like

@Marmiz Thank you for the warm welcome.

If I understand what you’re saying correctly, I assume that I have to call a function for the forEach statement to work?

If that’s the case, it makes more sense why nothing is happening, but unfortunately I’m not sure how else to edit the items in an array. I suppose I could make a new array variable and use convertArr.map, but I’ve had a suspicion recently that creating several new variables in a function is considered bad practice, and if I’m honest I’d like to learn to write code efficiently as well as understanding the concepts used in the lessons.

I sincerely appreciate the help!

I tried out the map statement that you suggested, and eventually came to this code, which passed. Thank you very much for the suggestion.

function convertHTML(str) {
  let convertArr = str.split("");
  return convertArr
  .map(target => {
    switch (target) {
      case "&":
      return target = "&amp;";
      break;
      case "<":
      return target = "&lt;";
      break;
      case ">":
      return target = "&gt;";
      break;
      case "\"":
      return target = "&quot;";
      break;
      case "\'":
      return target = "&apos;";
      break;
      default:
      return target;
      break;
    }
  })
  .join("");
}

console.log(convertHTML("Dolce & Gabbana"));

forEach is already calling a function on each element of the array for you, (more information on the documentation page linked)

Let’s consider this example:

goal: write a function that sum the number in an array and return the total.

pseudo-code:
 - take the array and loop it with a forEach
 - for each number add it to the sum
 - return final sum

Let’s try to convert it:

[1,2,3].forEach(number => total = number + prevuiousTotal)

// Uncaught ReferenceError: prevuiousTotal is not defined

How exactly we can keep track of the total, and update it on each iteration?
Well it could be another variable, but cannot live inside the callback else it would be overwritten on each iteration

let total = 0;
[1,2,3].forEach(number => total += number)
console.log(total) // 6 

This is a trivial example, but I hope it will help you :smile:

To convert it on you function, you have a function called on each letter, you are updating it, but where this updated value is going?

Glad you could make it work, hover I suggest you to go back and understand why a map worked and a forEach did not, and eventually understand how you could have done it with the latest.

Happy coding :sparkle:

Thank you very much, I’ll take a look at your explanation and learn more about forEach before moving on to the next problem.