The Problem
Create a function that recursively counts an integer’s number of digits.
The two tricks to solve this problem are:
- Get the absolute value of the number passed into the function, then convert it to a string. I used the
Math.abs
method and theString
constructor. - Count the digits of the number with an incrementer, in this case
nums
.
This is a problem that I completed on the site Edabit, made by Deep Xavier.
My Solution
//i: number
//o: number
//declare count, takes a number, nums default parameter zero
function count(number, nums = 0) {
//make number absolute with Math.abs, make the number a string
number = String(Math.abs(number));
//base case if sting.length === 0, return nums
if (number.length === 0) return nums;
//recursive case nums++, slice number to shorten
nums++;
console.log(nums)
number = number.slice(1);
console.log(number)
// return count with number and nums
//return count(number, nums);
}
console.log(count(318)); // 3
Another solution
This is what ChatGPT provided with the same prompt.
//This version gets the absolute value of the number, then if it's less than 10, will return 1. If greater than 10, it adds one to the recursive result of countDigits until it's less than 10.
function countDigits(n) {
if (n < 0) {
n = Math.abs(n);
}
if (n < 10) {
return 1;
}
return 1 + countDigits(Math.floor(n / 10));
}