We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functiontimeInWords(h,m){consttime=newMap();time.set(1,`one`);time.set(2,`two`);time.set(3,`three`);time.set(4,`four`);time.set(5,`five`);time.set(6,`six`);time.set(7,`seven`);time.set(8,`eight`);time.set(9,`nine`);time.set(10,`ten`);time.set(11,`eleven`);time.set(12,`twelve`);time.set(13,`thirteen`);time.set(14,`fourteen`);time.set(15,`quarter`);time.set(16,`sixteen`);time.set(17,`seventeen`);time.set(18,`eighteen`);time.set(19,`nineteen`);time.set(20,`twenty`);time.set(30,`half`);letpast_to;// holds "past" or "to"if(m<=30){past_to=`past`;}else{past_to=`to`;m=60-m;// convert minutes 0 to 29h=(h+1)%12;// adjust to next hour}letmin_s;// holds "minute", "minutes" or nothingif(m===15||m===30){min_s=``;}elseif(m===1){min_s=`minute`;}else{min_s=`minutes`;}lettwenty;// holds 2-words "twenty smth" or nothingif(m>20&&m<30){twenty='twenty';m=m%10;}else{twenty='';}lethour=time.get(h),min=time.get(m);returnm===0?`${hour}o'clock`:`${twenty}${min}${min_s}${past_to}${hour}`;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Time in Words
You are viewing a single comment's thread. Return to all comments →
JavaScript