Created
March 27, 2020 23:06
-
-
Save Nicknyr/44975ff87d8c5d6d6799a3b20b20059a to your computer and use it in GitHub Desktop.
CodeSignal - Array Replace
This file contains hidden or 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) { | |
| 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