ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้ตัวเลข ฟังก์ชันของเราควรส่งคืนอาร์เรย์ของสตริงที่มีตัวเลขที่ตัดในแต่ละหลัก
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const num = 246;
const cutOffEach = (num = 1) => {
const str = String(num);
const res = [];
let temp = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
temp += el;
res.push(temp);
};
return res;
};
console.log(cutOffEach(num)); ผลลัพธ์
ต่อไปนี้เป็นเอาต์พุตคอนโซล -
[ '2', '24', '246' ]