Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Created February 26, 2018 23:58
Show Gist options
  • Select an option

  • Save devNoiseConsulting/c5ac3aeaa1f14f03f61fa0ba24899497 to your computer and use it in GitHub Desktop.

Select an option

Save devNoiseConsulting/c5ac3aeaa1f14f03f61fa0ba24899497 to your computer and use it in GitHub Desktop.
Reverse Odd Runs - PhillyDev Slack #daily_programmer - 20180226
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