convertToMilliseconds.js (8kyu 54)
Codewars 알고리즘 풀이
Problem
- Your task is to make convertToMilliseconds() function which returns time converted to milliseconds.
- 밀리초로 변환된 시간을 반환한다.
Tips
1h = 60m = 3600s = 3600000ms
Solution 01
function convertToMilliseconds(h, m, s) {
let hoursToSec = h * 60 * 60;
let minutesToSec = m * 60;
return (hoursToSec + minutesToSec + s) * 1000;
}
convertToMilliseconds(0, 0, 1); // 1000
convertToMilliseconds(0, 1, 0); // 60000
convertToMilliseconds(1, 0, 0); // 3600000
convertToMilliseconds(1, 1, 1); // 3661000
Solution 02
function convertToMilliseconds(h, m, s) {
let hoursToMilli = h * 60 * 60 * 1000;
let minutesToMilli = m * 60 * 1000;
let secondsToMilli = s * 1000;
return hoursToMilli + minutesToMilli + secondsToMilli;
}
convertToMilliseconds(0, 0, 1); // 1000
convertToMilliseconds(0, 1, 0); // 60000
convertToMilliseconds(1, 0, 0); // 3600000
convertToMilliseconds(1, 1, 1); // 3661000
Solution 03
function convertToMilliseconds(h, m, s) {
return (h * 3600 + m * 60 + s) * 1000;
}
convertToMilliseconds(0, 0, 1); // 1000
convertToMilliseconds(0, 1, 0); // 60000
convertToMilliseconds(1, 0, 0); // 3600000
convertToMilliseconds(1, 1, 1); // 3661000