Skip to content

Instantly share code, notes, and snippets.

@JustAyush
Created January 16, 2021 10:49
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 JustAyush/5e8642676582fc77148bc8dc6a2c2abe to your computer and use it in GitHub Desktop.
Save JustAyush/5e8642676582fc77148bc8dc6a2c2abe to your computer and use it in GitHub Desktop.
function super_digit(n, k) {
const p = find_p(n, k);
const superDigit = find_super_digit(p);
return superDigit;
}
function find_p(n, k) {
let concatString = "";
for (let i = 0; i < k; i++) {
concatString = concatString + n.toString();
}
return concatString;
}
function find_super_digit(p) {
if (p.length === 1) return +p;
let arrayOfDigits = p.split("");
let sum = arrayOfDigits.reduce((total, current) => total + (+current), 0);
sum = sum.toString();
return find_super_digit(sum.toString());
}
const superDigit = super_digit(148, 3);
console.log(superDigit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment