Newbie help please

Hi people. I’m working through the loops section and wanted to see if I could make my own function (I think I learn better this way).

I want to make a function that will simply time. For example, if you put in 120 seconds it will return 2 minutes.

This is the beginning of what I have:

let seconds = 0;
let minutes = 0;
let hours = 0;
let days = 0;
let weeks = 0;
let months = 0;
let years = 0;

function howManyMinutes(amountSeconds) {
let i = amountSeconds;
while (i > 60) {
i - 60;
minutes++;
if (i < 60) {
i = seconds;
return amountSeconds + ’ is equal to ’ + minutes + ’ minutes and ’ + seconds + ’ seconds.’;
}
}
}

console.log(howManyMinutes(120));

However, when I try to run this in Terminal it seems to keep running leading me to assume that I must have made an infinite loop somewhere, or otherwise just done something completely wrong. Any advice would be very loved!!

2 Likes

Hey,

You are adding to your minutes, that’s good, but you are not deducting i at each step.
Thus whilst i does not change the while loop will always meet the condition i > 60 so it will run forever.

You will need something like this:

i = i - 60;

Now for the next part, assigning the remaining seconds

i = seconds;

This is the wrong way round. You now say i is equal to 0 instead of seconds is equal to the value of i :wink:

1 Like

Btw you now do not take in account the condition when i has the value of 60.

1 Like

Thank you, that all makes perfect sense, and has saved me a headache! I’ll get cracking. Thank you very much :slight_smile:

2 Likes

Sure no problem, anytime :wink: . Happy coding!

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.