Created
April 13, 2020 19:42
-
-
Save Nicknyr/8bf5f1517e6acdfa6acca4b7c5ba13e8 to your computer and use it in GitHub Desktop.
CodeSignal - Array Replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. | |
Example | |
For inputArray = [1, 2, 1], elemToReplace = 1, and substitutionElem = 3, the output should be | |
arrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3]. | |
*/ | |
function arrayReplace(inputArray, elemToReplace, substitutionElem) { | |
for(let i = 0; i < inputArray.length; i++) { | |
if(inputArray[i] === elemToReplace) { | |
inputArray[i] = substitutionElem; | |
} | |
} | |
return inputArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment