//4kyu Human readable duration format
/*
написать функцию, которая форматирует длительность,
заданную в виде количества секунд, удобным для человека способом.
1.Функция должна принимать неотрицательное целое число
2.Если на вход 0, то return "now"
3.Если не 0, то return комбинация years, days, hours, minutes and seconds
=== пример 3662 = "1 hour, 1 minute and 2 seconds" ===
4. Если число > 1, то слово во множественном числе (1 second, 2 seconds)
5. Слова разделяются запятой, кроме последнего слова, должно быть and (1 hour, 1 minute and 2 seconds)
6. years -> days -> hours -> minutes -> seconds
*/
function formatDuration(sec) {
let timeWords = ["year", "day", "hour", "minute", "second"]
if (sec === 0 ) return "now";
if (sec < 0 ) return -1;
let obj = {};
//получаем годы
let years = Math.floor(sec / 3600 / 24 / 365)
console.log(years);
//получаем дни
let days = Math.floor(sec / 3600 / 24 % 365);
console.log(days);
//получаем часы
// let hours = Math.floor(sec / 3600);
// console.log(hours);
let hours = Math.floor(sec / 3600 % 24);
console.log(hours);
//считаем остаток
let quantitySeconds = sec % 3600;
console.log(quantitySeconds);
//получаем минуты
let minutes = Math.floor(quantitySeconds / 60);
console.log(minutes);
let seconds = quantitySeconds % 60;
console.log(seconds);
if (years !== 0) years > 1 ? obj["years"] = years : obj["year"] = years;
if (days !== 0) days > 1 ? obj["days"] = days : obj["day"] = days;
if (hours !== 0) hours > 1 ? obj["hours"] = hours : obj["hour"] = hours;
if (minutes !== 0) minutes > 1 ? obj["minutes"] = minutes : obj["minute"] = minutes;
if (seconds !== 0) seconds > 1 ? obj["seconds"] = seconds : obj["second"] = seconds;
console.log(obj);
console.log(obj);
let newString = "";
for(let [key, value] of Object.entries(obj)) {
newString += value + " " + key + ', ';
}
console.log(Object.keys(obj).length);
if (Object.keys(obj).length > 1) {
return newString.split(',').map((item, index) => {
if (index === newString.split(',').length - 2) {
return item = item.padStart(item.length + 4, ' and');
}
else if (index === newString.split(',').length - 3) return item;
else if (index === newString.split(',').length - 1) return item;
else return item + ',';
}).join('').trim();
}
else {
return newString.split(',').join('').trim();
}
}