Skip to content

Instantly share code, notes, and snippets.

@renatocassino
Last active August 11, 2018 23:10
Show Gist options
  • Save renatocassino/382ec852af2c6fdba2064a1707d80baf to your computer and use it in GitHub Desktop.
Save renatocassino/382ec852af2c6fdba2064a1707d80baf to your computer and use it in GitHub Desktop.
2048-moveLineUp.js
const moveLineUp = (line) => {
// Internal function to recursive
const moveLineUpInner = (currentLine, results=[]) => {
if (currentLine.length === 0) return results;
if(currentLine[0] === currentLine[1]) {
const total = currentLine[0] + currentLine[1];
return moveLineUpInner(currentLine.slice(2), [...results, total]);
}
return moveLineUpInner(currentLine.slice(1), [...results, currentLine[0]]);
};
return arrayPad(moveLineUpInner(line.filter((n)=>n > 0)), line.length); // Array pad here
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment