Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.
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
62 changes: 60 additions & 2 deletions countdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
function countdown(seconds){
// ...
// Levels
// Note: All levels wait a second before starting the countdown

// 3. BONUS: don't define any new variables

function countdown(seconds) {
setTimeout(timer,1000,seconds);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function necessary?


function timer(seconds) {
if ( seconds > 0 ) {
document.write("<br/>" + seconds + "...");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document.write can be a form of eval.

} else {
document.write("<br/>" + seconds);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document.write can be a form of eval.

}
seconds--;
if ( seconds >= 0 ) {
setTimeout(timer,1000,seconds);
}
}

// 2. Keep track of time without defining any global variables

// var intervalID;

// function countdown(seconds) {
// var counter = {
// total : seconds
// };
// intervalID = setInterval(timer,1000,counter);
// }

// function timer(counter) {
// if ( counter.total === 0 ) {
// document.write("<br/>" + counter.total);
// clearInterval(intervalID);
// } else {
// document.write("<br/>" + counter.total + "...");
// counter.total--;
// }
// }

// 1. Use global variable to keep track of time

// var counter;
// var intervalID;

// function countdown(seconds) {
// counter = seconds;
// intervalID = setInterval(timer,1000);
// }

// function timer() {
// if ( counter === 0 ) {
// document.write("<br/>" + counter);
// clearInterval(intervalID);
// } else {
// document.write("<br/>" + counter + "...");
// counter--;
// }
// }

countdown(5);