Last active
August 11, 2018 23:10
-
-
Save renatocassino/382ec852af2c6fdba2064a1707d80baf to your computer and use it in GitHub Desktop.
2048-moveLineUp.js
This file contains 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 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