Skip to content

Instantly share code, notes, and snippets.

@KEIII
Last active February 22, 2023 19:35
Show Gist options
  • Save KEIII/6e6d3e0eac190d362af101e91fa4d5c5 to your computer and use it in GitHub Desktop.
Save KEIII/6e6d3e0eac190d362af101e91fa4d5c5 to your computer and use it in GitHub Desktop.
// hack
const f1 = (n) => String(n).length;
// log10
const f2 = (n) => Math.floor(Math.log10(n) + 1);
// recursive
const f3 = (n) => {
if (n < 10) return 1;
return 1 + fc(n / 10);
};
// while
const f4 = (n) => {
if (n === 0) {
return 1;
}
let result = 0;
while (n > 0) {
n = Math.trunc(n / 10);
result++;
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment