I have a function that takes a time of minutes and seconds, and converts it to a string in the following way: '0:30' would become '30 Seconds', '1:30' would become '1 minute 30 seconds', '2:01' would become '2 minutes 1 second', etc. My function works fine, but I'm wondering if there are areas for improvement.
function formatTime(formattedTime) {
let minStr, secStr;
let minNum = formattedTime.split(':')[0];
let secNum = formattedTime.split(':')[1];
if (minNum === '1') {
minStr = 'minute';
} else {
minStr = 'minutes';
}
if (secNum === '01') {
secStr = 'second';
secNum = '1';
} else {
secStr = 'seconds';
}
if (minNum === '0') {
return `${secNum} ${secStr}`;
} else if (secNum === '00') {
return `${minNum} ${minStr}`;
} else {
return `${minNum} ${minStr} ${secNum} ${secStr}`;
}
}
minNum
is easily confused with "minimum number", why not simply writeminutes
instead? \$\endgroup\$minutes = 'minutes'
\$\endgroup\$