ปัญหา
ระบบจดทะเบียนรถของเมือง N มอบหมายเลขสองประเภท:
Customer ID - จำนวนธรรมชาติระหว่าง 0 ถึง 17558423 ให้กับผู้ซื้อรถตามลำดับ: ลูกค้าคนแรกได้ ID 0, คนที่สองได้ 1, คนที่สามได้ 2, ...
Number Plate - ให้กับรถ ประกอบด้วย series (ตัวอักษรพิมพ์เล็ก 3 ตัว a-z) และ serial number (ตัวเลข 3 หลัก 0-9) ตัวอย่าง: aaa001 แต่ละ Number Plate เกี่ยวข้องกับ Customer ID ที่กำหนด เช่น aaa001 -> ID 0, aaa002 -> ID 1, ...
เราต้องเขียนฟังก์ชัน JavaScript ที่รับ customerID และคำนวณ Number Plate ที่สอดคล้อง แล้วคืนเป็นสตริง
ตัวอย่าง
โค้ดด้านล่างนี้
const id = 545664;
const findNumberPlate = (id = 0) => {
const letters = 'abcdefghijklmnopqrstuvwxyz';
let num = String(id % 999 + 1);
if(num.length !== 3);
while(num.length !== 3){
num = '0' + num;
}
const l = Math.floor(id / 999);
return letters[l % 26] + letters[(Math.floor(l / 26)) % 26] + letters[(Math.floor(l / (26 * 26))) % 26] + num;
};
console.log(findNumberPlate(id));ผลลัพธ์
ผลลัพธ์ในคอนโซลคือ
ava211