# Switch Case in a forEach function? Code not taking effect

**URL:** <https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799>\
**Category:** General\
**Created:** [January 27, 2020, 1:39am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799 "2020-01-27T01:39:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [January 27, 2020, 1:39am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/1 "2020-01-27T01:39:09Z")

</div>

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.

```auto
function convertHTML(str) {
  let convertArr = str.split("");
  convertArr.forEach(target => {
    switch (target) {
      case "&":
      target = "&amp;";
      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"));

```

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [January 27, 2020, 2:29am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/2 "2020-01-27T02:29:38Z")

</div>

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 👍

---

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [January 27, 2020, 2:37am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/3 "2020-01-27T02:37:01Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [January 27, 2020, 2:57am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/4 "2020-01-27T02:57:19Z")

</div>

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

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [January 27, 2020, 2:59am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/5 "2020-01-27T02:59:34Z")

</div>

> [@AndrewGarrison](#):
>
> I assume that I have to call a function for the forEach statement to work?

[forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/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:

```markdown
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:

```auto
[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

```auto
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 😄

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?

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [January 27, 2020, 3:01am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/6 "2020-01-27T03:01:32Z")

</div>

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 ❇

---

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [January 27, 2020, 3:12am UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/7 "2020-01-27T03:12:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:37pm UTC](https://forum.freecodecamp.org/t/switch-case-in-a-foreach-function-code-not-taking-effect/344799/8 "2021-06-01T19:37:17Z")

</div>

Closed on June 1, 2021, 7:37pm UTC


