ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่ใช้ตัวเลขเป็นไมล์/แกลลอน และคืนค่า km/ลิตร ที่เทียบเท่ากัน
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
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