Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created April 13, 2020 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicknyr/8bf5f1517e6acdfa6acca4b7c5ba13e8 to your computer and use it in GitHub Desktop.
Save Nicknyr/8bf5f1517e6acdfa6acca4b7c5ba13e8 to your computer and use it in GitHub Desktop.
CodeSignal - Array Replace
/*
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