Digit Count
Programming Basics · JavaScript
TASK
Problem
Given a non-negative integer up to 999999, print its digit count. Zero has one digit.
EXAMPLE
Example
10500
5
LIMITS
Constraints
`n` is an integer from `0` to `999999`; zero is considered a one-digit number.
LEARN
Theory for this problem
+
### Digit Count
Integer division separates higher decimal/time units, while `%` returns the remainder. These two operations let a program isolate digits or split a total number of seconds/minutes into components.
NEED HELP?
Hints
+
['Decide which part comes from division and which part comes from the remainder.', 'Do not mix the loop counter with the accumulated answer; those variables serve different roles.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let n = Number(input);
if (n < 10) {
console.log(1);
} else {
if (n < 100) {
console.log(2);
} else {
if (n < 1000) {
console.log(3);
} else {
if (n < 10000) {
console.log(4);
} else {
if (n < 100000) {
console.log(5);
} else {
console.log(6);
}
}
}
}
}