ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่รับตัวเลขในหน่วย miles/gallon และคืนค่าเทียบเท่าใน km/litre
ตัวอย่าง
โค้ดด้านล่างนี้
const num = 25;
const converter = (mpg) => {
let LITRES_PER_GALLON = 4.54609188;
let KILOMETERS_PER_MILE = 1.609344;
const ratio = KILOMETERS_PER_MILE / LITRES_PER_GALLON;
return Math.round(100 * mpg * ratio) / 100;
};
console.log(converter(num));ผลลัพธ์
ผลลัพธ์ในคอนโซลคือ
8.85