Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created March 27, 2020 23:06
Show Gist options
  • Select an option

  • Save Nicknyr/44975ff87d8c5d6d6799a3b20b20059a to your computer and use it in GitHub Desktop.

Select an option

Save Nicknyr/44975ff87d8c5d6d6799a3b20b20059a 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) {
let answer = inputArray.reduce((a, element, index) => {
if(element == elemToReplace)
element = substitutionElem;
a.push(element);
return a;
}, []);
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment