There is an array arr = [1, 2, "a", 4, 5];. Using loop (for...of) every element of array must be pushed inside another empty array result = [];.
But, inside arr there is an element that needed to be replaced inside loop. Here, we need to replace string "a" with number 3 and than push it inside array result.
In the end we need to get an array result = [1, 2, 3, 4, 5];. As you can see string "a" was replaced and instead number 3 was pushed.
!Important initial array arr shouldn't be mutated and loop is a required condition.
Example:
const arr = [1, 2, "a", 4, 5];
const result = [];
for(let elem of arr) {
if(typeof elem === "string") {
// changing elem to number 3 and pushing it into result array instead of "a"
}
// ...
}
Tried use several different array methods, but it didn't help.
for (let i = 0; i < arr.length; i++)if u want acces to index. or bothmapandforEachalso gives access to index