Replacing Array Item
Given an array
Sample Input 1
[1, 2, 3, "four", 5, 6]
"four"
4
Sample Output 1
[ 1, 2, 3, 4, 5, 6 ]
Sample Input 2
[22, 44, 88, 352, 352]
352
176
Sample Output 2
[ 22, 44, 88, 176, 352 ]
function replace(myArray, targetItem, replaceItem) {
for (let i = 0; i < myArray.length; i++) {
if (myArray[i] === targetItem) {
myArray[i] = replaceItem;
break;
}
}
}
Comments
Leave a comment