Skip to content

Instantly share code, notes, and snippets.

@tevko
Created March 8, 2021 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevko/601da6e7190ce77f8b529380972c1579 to your computer and use it in GitHub Desktop.
Save tevko/601da6e7190ce77f8b529380972c1579 to your computer and use it in GitHub Desktop.
// https://www.codewars.com/kata/556206664efbe6376700005c/solutions/javascript/me/best_practice
const isPolydivisible = (n, b) => n.toString().split('').every(
(d, i, a) => (a.join('').slice(0, i + 1))
.split('').reverse()
.map(num => Number(num) || CHARS.indexOf(num))
.reduce((a, c, i) =>
(c * (Math.pow(b, i))) + Number(a)
)
% (i + 1) === 0
);
const getPolydivisible = (n, b) => {
const convertToBaseFromTen = (num, base, concat = '') => {
if (Math.floor(num / base) >= 1) {
return convertToBaseFromTen(Math.floor(num / base), base, concat + (num % base > 9 ? CHARS[num % base] : num % base))
} else {
return (concat + Math.floor(num % base)).split('').reverse().join('')
}
};
const deducer = n >= b ? b : 0;
let inc = deducer;
let dec = n;
let highestPoly;
while (dec > deducer) {
const numInBase = convertToBaseFromTen(inc, b);
if (isPolydivisible(numInBase, b)) {
highestPoly = numInBase;
dec--;
}
inc++;
}
return n <= b ? CHARS[n - 1] : highestPoly;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment