From 8b798d429b990aa004271d19b2aa0093c9a14ec6 Mon Sep 17 00:00:00 2001 From: koronya Date: Wed, 13 May 2026 04:51:03 +0900 Subject: [PATCH] [JS][7kyu] When's the next train to Brighton? --- .../koronya.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 codewars/7kyu/when-s-the-next-train-to-brighton/koronya.js diff --git a/codewars/7kyu/when-s-the-next-train-to-brighton/koronya.js b/codewars/7kyu/when-s-the-next-train-to-brighton/koronya.js new file mode 100644 index 000000000..72d9172af --- /dev/null +++ b/codewars/7kyu/when-s-the-next-train-to-brighton/koronya.js @@ -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.'