count number of digits coding interview question
//Calculate the number of digits present in the integer
function countDigits(num){
let result = 0;
num = Math.abs(num);
while(num > 0){
result++;
num = Math.floor(num/10);
}
return result;
}
function countDigits1(num){
return Math.abs(num).toString().length
}
function countDigits2(num){
return (""+Math.abs(num)).length
}
console.log(countDigits1(-12345)); // 5
console.log(countDigits1(6258)); // 4
console.log(countDigits1(-1)); // 1
console.log(countDigits1(1587452385)); // 10