Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Yuval Yarmus
1,020 PointsMaking a countdown of 6 minutes in JS
Hey, guys, I am trying to make a countdown from 6 minutes in JS. My code is: var time = 360; // 360 seconds is 6 minutes ( 60 * 6 = 360) var minutes = Math.Floor(time / 60); var seconds = time % 60; for some reason minutes never get off 6 minutes and seconds is always zero. The only thing that works well is the time update. // note: I added a line of time = time - 1 and put that in an interval that repeats every second.
2 Answers

Camilo Lucero
27,692 PointsVariables store a value at the moment they are created, they don't change dynamically or recalculate themselves every time you call them. That is why your seconds variable is always zero, because even if time changes, the variable remains with the same value (360 % 60 = 0).
You need to recalculate the variables inside of the function in the interval, so they are reassigned to the new value every second.
var time = 360;
function countdown() {
var minutes = Math.floor(time / 60);
var seconds = time % 60;
document.body.innerHTML = minutes + ':' + seconds; // prints the countdown in the HTML
time = time - 1;
}
setInterval(countdown, 1000);

Yuval Yarmus
1,020 PointsI already finished this project and I made it work in the end by the idea was that the entire function repaeats itself every second and every second time's value is substracted by 1