So, I have a simple task, given the array: let arr = [true, false, true, false, true]; I need to reverse true to false and visa versa. I have managed to do this with a for loop: And it works fine.
Now, I'm trying to do the same with a forEach and I can't figure out why this won't work. So, here's my code:
for (let i = 0; i < arr.length; i++) {
if (arr[i] === true) arr[i] = false;
else arr[i] = true;
} // it works
// for some reason, this doesn't
arr2.forEach(el => el === true ? el = false : el = true);
console.log(arr)
//Neither this:
arr.forEach(el => el === true && el = false || el === false && el = true);
console.log(arr)
The map doesn't work either: Can someone point out my mistake and explain what I am doing wrong? Maybe show other ways to solve it? With filter, reduce or which is more preferrable? A one-liner solution is highly preferred. Thank you for your answers!