constformat_duration=number=>{if(parseInt(number)&&number>0){// breakpoints: 60 seconds in a minute, 3600 seconds in an hour, etc.constbp=[60,3600,86400,31536000,Number.MAX_VALUE];constunits=["second",'minute','hour','day','year'];letsolution=[];for(letx=0;x<bp.length;x++){if(number%bp[x]!==0){constvalue=Math.floor((number%bp[x])/(bp[x-1]||1));solution.push(`${value}${units[x]}${value>1?'s':''}`);number-=number%bp[x];}}// edge case: the value was too large and the modulus was considered 0if(number>0)solution.push(`${number} years`)// add the "and" for the last element (because Oxford comma and cheating)solution[0]="and "+solution[0];returnsolution.reverse().join(', ');}return"Invalid number";}
With a little bit of cheating: instead of replacing the last comma with an "and", I just add the "and" to the last element and claim that everyone should be using Oxford comma to avoid misunderstandings.
Cool.i like the way you explain things man. Just want to be in touch with you to ask some basic doubta as im a beginner. Is there any other way i can be in touch. Do u have wssap? I wont bother u much.dont be scared😅
I'm not going to lie: I've had bad experiences with this in the past. If you have questions and you post them here or in StackOverflow, I'll be happy to look at them and answer if I know the answer.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript
With a little bit of cheating: instead of replacing the last comma with an "and", I just add the "and" to the last element and claim that everyone should be using Oxford comma to avoid misunderstandings.
Live demo on CodePen.
This feels overcomplicated. It probably can be done easier with a
map
or areduce
. I'll check later with more time.Hello Alvaro. Did u get a chance to work on this?🤓
LOL. I didn't 😓😬
Thats fine Alvaro. Are u on discord??
Not really. I have only used once or twice.
Cool.i like the way you explain things man. Just want to be in touch with you to ask some basic doubta as im a beginner. Is there any other way i can be in touch. Do u have wssap? I wont bother u much.dont be scared😅
I'm not going to lie: I've had bad experiences with this in the past. If you have questions and you post them here or in StackOverflow, I'll be happy to look at them and answer if I know the answer.