Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Countdown Homework #58

Open
wants to merge 4 commits into
base: gh-pages
Choose a base branch
from
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);