Created
February 26, 2018 23:58
-
-
Save devNoiseConsulting/c5ac3aeaa1f14f03f61fa0ba24899497 to your computer and use it in GitHub Desktop.
Reverse Odd Runs - PhillyDev Slack #daily_programmer - 20180226
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
| const reverseOddRuns = function(...numbers) { | |
| let tempStorage = []; | |
| return numbers | |
| .reduce((acc, v, i) => { | |
| if (v % 2 == 0) { | |
| acc = acc.concat([...tempStorage.reverse(), v]); | |
| tempStorage = []; | |
| } else { | |
| tempStorage.push(v); | |
| } | |
| return acc; | |
| }, []) | |
| .concat(tempStorage.reverse()); | |
| }; | |
| let test = [1, 2, 3]; | |
| let result = reverseOddRuns(...test); | |
| // 1, 2, 3 | |
| console.log(result); | |
| test = [1, 3, 2]; | |
| result = reverseOddRuns(...test); | |
| // 3, 1, 2 | |
| console.log(result); | |
| test = [10, 7, 9, 6, 8, 9, 7]; | |
| result = reverseOddRuns(...test); | |
| // 10, 9, 7, 6, 8, 7, 9 | |
| console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment