# Code Review - Calculate the following second (JavaScript)

**URL:** <https://forum.freecodecamp.org/t/code-review-calculate-the-following-second-javascript/398020>\
**Category:** I Built This\
**Created:** [June 3, 2020, 12:44am UTC](https://forum.freecodecamp.org/t/code-review-calculate-the-following-second-javascript/398020 "2020-06-03T00:44:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Softlink](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/softlink/32/134391_2.png) [@Softlink](https://forum.freecodecamp.org/u/Softlink)\
**Post date:** [June 3, 2020, 12:44am UTC](https://forum.freecodecamp.org/t/code-review-calculate-the-following-second-javascript/398020/1 "2020-06-03T00:44:17Z")

</div>

As a newbie coder I’d be grateful for some feedback on how my code could be improved.

It’s my answer to the question below, taken from “The JavaScript Way” chapter 3 (available for free [https://github.com/thejsway/thejsway](https://github.com/thejsway/thejsway)) The only tools we’ve been given to solve this are the basics; variables, conditions, math operators.

Question -  
Following second  
Write a program that asks for a time under the form of three information (hours, minutes, seconds).  
The program calculates and shows the time one second after. Incorrect inputs must  
be taken into account.

This is not as simple as it seems… Look at the following results to see for yourself:  
• 14h17m59s ⇒ 14h18m0s  
• 6h59m59s ⇒ 7h0m0s  
• 23h59m59s ⇒ 0h0m0s (midnight)

```auto
let hour = Number(prompt("what hour is it?"));
let minute = Number(prompt("what minute is it?"));
let second = Number(prompt("what second is it?"));

if (second > 59 || second < 0 || minute > 59 || minute < 0 || hour > 23 || hour < 0) 
{
    console.log("please, enter a valid time");
} else if ((hour === 23) && (minute === 59) && (second === 59)) {
    hour = 0;
    minute = 0;
    second = 0;
    console.log(`${hour}h ${minute}m ${second}s`);

} else if ((second <= 59) && (minute <= 59) && (hour <= 23)) {
    hour++;
    minute =0;
    second =0;
    console.log(`${hour}h ${minute}m ${second}s`);

} else if (second < 59 && hour <= 23 && minute <= 59) {
    second++;
    console.log(`${hour}h ${minute}m ${second}s`);

}

else if (second <= 59) {
    second = 0;
    minute++;
    console.log(`${hour}h ${minute}m ${second}s`);
} 

```

---

<div class="post-metadata">

**Author:** ![Roma](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/roma/32/112899_2.png) [@Roma](https://forum.freecodecamp.org/u/Roma)\
**Post date:** [June 8, 2020, 3:22pm UTC](https://forum.freecodecamp.org/t/code-review-calculate-the-following-second-javascript/398020/2 "2020-06-08T15:22:52Z")

</div>

> [@Softlink](#):
>
> This is not as simple as it seems… Look at the following results to see for yourself:  
> • 14h17m59s ⇒ 14h18m0s  
> • 6h59m59s ⇒ 7h0m0s  
> • 23h59m59s ⇒ 0h0m0s (midnight)

Your logic is off. Going by the above, your code doesn’t meet the criteria. If I put in `14h17m59s`, your code outputs `15h 0m 0s`

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:16pm UTC](https://forum.freecodecamp.org/t/code-review-calculate-the-following-second-javascript/398020/3 "2021-06-01T13:16:13Z")

</div>

Closed on June 1, 2021, 1:16pm UTC


