If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.
If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.
Kata #30
DESCRIPTION:
Clock shows h hours, m minutes and s seconds after midnight.
Your task is to write a function which returns the time since midnight in milliseconds.
Example:
h = 0
m = 1
s = 1
result = 61000
Input constraints:
0 <= h <= 230 <= m <= 590 <= s <= 59
Starting code:
function past(h, m, s){
//#Happy Coding! ^_^
}
My attempt:
const past = (h, m, s) => (((h*3600)+(m*60)+s)*1000)
Rank #1 in 'Best Practice':
function past(h, m, s){
return ((h*3600)+(m*60)+s)*1000;
}
Rank #2:
const past = (h,m,s) => 1000 * (3600 * h + 60 * m + s);
Rank #3:
function past(h, m, s){
var miliseconds = 0;
miliseconds = miliseconds + s * 1000;
miliseconds = miliseconds + m * 60000;
miliseconds = miliseconds + h * 3600000;
return miliseconds;
}
Rank #4:
function past(h, m, s){
var hours = h * 60 * 60 * 1000;
var minutes = m * 60 * 1000;
var seconds = s * 1000;
return hours + minutes + seconds;
}
Rank #5 with the use of the Date object and .setHours() method:
function past(h, m, s){
const setTime = new Date().setHours(h, m, s);
const midnight = new Date().setHours(0, 0, 0);
return Math.round(setTime - midnight);
}
That's it for Kata #30, stay tuned for more katas to be solved!
Link to Kata #1: Square(n) Sum
Link to Kata #2: Convert a Number to a String
Link to Kata #3: DNA to RNA Conversion
Link to Kata #4: Remove First and Last Character
Link to Kata #5: MakeUpperCase
Link to Kata #6: Total amount of points
Link to Kata #7: A Needle in the Haystack
Link to Kata #8: Sum of positive
Link to Kata #9: Basic Mathematical Operations
Link to Kata #10: Beginner - Reduce but Grow
Link to Kata #11: Square Every Digit
Link to Kata #12: Friend or Foe?
Link to Kata #13: Grasshopper - Summation
Link to Kata #14: Get the Middle Character
Link to Kata #15: Descending Order
Link to Kata #16: String ends with?
Link to Kata #17: Sum of two lowest positive integers
Link to Kata #18: Sum of odd numbers
Link to Kata #19: Find the next perfect square!
Link to Kata #20: Reversed Strings
Link to Kata #21: Jaden Casing Strings
Link to Kata #23: Returning Strings
Link to Kata #24: Opposite number
Link to Kata #25: Are You Playing Banjo?
Link to Kata #26: Beginner Series #1 School Paperwork
Link to Kata #27: Remove String Spaces