Skip to content

Instantly share code, notes, and snippets.

@moondef
Created June 1, 2018 08:48
Show Gist options
  • Save moondef/694748168157b5f13f0d044e987686d2 to your computer and use it in GitHub Desktop.
Save moondef/694748168157b5f13f0d044e987686d2 to your computer and use it in GitHub Desktop.
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