เราจำเป็นต้องเขียนฟังก์ชันเรียกซ้ำของ JavaScript ที่รับตัวเลขและส่งคืนตัวเลขที่มากที่สุดในตัวเลข
ตัวอย่างเช่น หากตัวเลขคือ −
45654356
แล้วค่าที่ส่งคืนควรเป็น 6
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const num = 45654356;
const greatestDigit = (num = 0, greatest = 0) => {
if(num){
const max = Math.max(num % 10, greatest);
return greatestDigit(Math.floor(num / 10), max);
};
return greatest;
};
console.log(greatestDigit(num)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
6