Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions codewars/7kyu/when-s-the-next-train-to-brighton/koronya.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// [JS][7kyu] When's the next train to Brighton?
// when-s-the-next-train-to-brighton
// https://www.codewars.com/kata/5a07620780171f61ff0000cb/train/javascript

const nextTrain = (time, minutes) => {
const minutesNum = Number(minutes)
let [h, m] = time.split(':').map(Number)
const minuteSum = m + minutesNum
m = minuteSum % 60
if (minuteSum >= 60) {
h += Math.floor(minuteSum / 60)
}

if (m > 30) {
if (h >= 23 || h < 5) {
return 'The next train is at 05:00.'
}
h += 1
return `The next train is at ${h.toString().padStart(2, '0')}:00.`
} else {
if (h > 23 || h < 5) {
return 'The next train is at 05:00.'
}
return `The next train is at ${h.toString().padStart(2, '0')}:30.`
}
}

nextTrain('09:35', '10') === 'The next train is at 10:00.'
nextTrain('17:40', '30') === 'The next train is at 18:30.'
nextTrain('4:15', '60') === 'The next train is at 05:30.'