यहां आवश्यकताएं सरल हैं, हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक संख्या लेता है और उसमें अंकों की संख्या देता है।
उदाहरण के लिए -
The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3
आइए इस फ़ंक्शन के लिए कोड लिखें -
उदाहरण
const num = 2353454;
const digits = (num, count = 0) => {
if(num){
return digits(Math.floor(num / 10), ++count);
};
return count;
};
console.log(digits(num));
console.log(digits(123456));
console.log(digits(53453));
console.log(digits(5334534534)); आउटपुट
कंसोल में आउटपुट होगा -
7 6 5 10