Skip to content

Instantly share code, notes, and snippets.

@AndrewBestbier
Created January 8, 2018 10:54
Show Gist options
  • Save AndrewBestbier/7614759f18fa55a3370d9cb6cee13d36 to your computer and use it in GitHub Desktop.
Save AndrewBestbier/7614759f18fa55a3370d9cb6cee13d36 to your computer and use it in GitHub Desktop.
My solution
const values = [1, 2, 3, 'a', 'b', 'c'];
// [] -> []
// [x] -> [x]
// x: xs : y -> [x, y] ++ func(xs)
const melder = values => {
if (values.length <= 1) {
return values;
}
const head = values[0];
const tail = values[values.length - 1];
const body = values.slice(1, values.length - 1);
return [head, tail].concat(melder(body));
};
const res = melder(values);
console.log('res', res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment