ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้จำนวนวินาทีและคืนค่าจำนวนชั่วโมงและจำนวนนาทีที่มีอยู่ในวินาทีเหล่านั้น
ป้อนข้อมูล
const seconds = 3601;
ผลผลิต
const output = "1 hour(s) and 0 minute(s)";
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const seconds = 3601;
const toTime = (seconds = 60) => {
const hR = 3600;
const mR = 60;
let h = parseInt(seconds / hR);
let m = parseInt((seconds - (h * 3600)) / mR);
let res = '';
res += (`${h} hour(s) and ${m} minute(s)`)
return res;
};
console.log(toTime(seconds)); ผลลัพธ์
"1 hour(s) and 0 minute(s)"