Count the digits
- O(log n) · Easy
- Free
- Python
- JavaScript
- loops
- operations
Problem
Write a function that takes a whole number and returns how many digits it has: 405 has 3.
The idea is a while that divides the number by 10 until it reaches 0, counting how many times it did. Each division takes one digit away.
The number 0 has 1 digit, even though the loop never runs.
From a negative number you count its digits without the sign: -405 has 3.
Examples
7 → 1
405 → 3
100000 → 6
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def count_digits(n):
passJavaScript
function countDigits(n) {
}Solve this challenge
It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.
More O(log n) challenges
- Digits to number the pagesPython · JavaScript · digits · operations · loops
- Split the thousandsPython · JavaScript · strings · digits · loops
- The ad with no pairPython · JavaScript · lists · search · indexes
- The beacons that are missingPython · JavaScript · lists · search · indexes
- The catches of the same weightPython · JavaScript · lists · search · indexes
- The first broken buildPython · JavaScript · lists · search · booleans