Created
June 1, 2018 08:48
-
-
Save moondef/694748168157b5f13f0d044e987686d2 to your computer and use it in GitHub Desktop.
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 balancedNum = number => { | |
const str = number.toString(); | |
const getMiddle = s => { | |
const splited = s.split(''); | |
if (splited.length % 2 === 0) { | |
return splited[splited.length / 2 - 1] + splited[splited.length / 2]; | |
} else { | |
return splited[Math.floor(splited.length / 2)]; | |
} | |
} | |
const sum = s => { | |
if(s.length) { | |
return s.split('').reduce((acc, curr) => parseInt(acc) + parseInt(curr)); | |
} else { | |
return 0; | |
} | |
} | |
const [ left, right ] = str.split(getMiddle(str)); | |
return sum(left) === sum(right) | |
? 'Balanced' | |
: 'Not Balanced'; | |
} | |
console.log(balancedNum(56239814)) // Balanced |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment