From da96cf7c9497feba093c434ec81baaa1b55e7de3 Mon Sep 17 00:00:00 2001 From: bluemittens505 Date: Sat, 6 Feb 2016 22:19:15 -0500 Subject: [PATCH 1/4] Level 1 Use global variable to keep track of time --- countdown.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/countdown.js b/countdown.js index 3001c8d..859dbfa 100644 --- a/countdown.js +++ b/countdown.js @@ -1,5 +1,22 @@ +// Levels +// 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("
" + counter); + clearInterval(intervalID); + } else { + document.write("
" + counter + "..."); + counter--; + } } countdown(5); From 669bf47a9a20f436b5454cdd80d1de93da84fa16 Mon Sep 17 00:00:00 2001 From: bluemittens505 Date: Sat, 6 Feb 2016 23:00:40 -0500 Subject: [PATCH 2/4] Level 3 BONUS: don't define any new variables Still need to do level 2 --- countdown.js | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/countdown.js b/countdown.js index 859dbfa..6cf462b 100644 --- a/countdown.js +++ b/countdown.js @@ -1,22 +1,44 @@ // Levels -// 1. Use global variable to keep track of time +// Note: All levels wait a second before starting the countdown -var counter; -var intervalID; +// 3. BONUS: don't define any new variables -function countdown(seconds){ - counter = seconds; - intervalID = setInterval(timer,1000); +function countdown(seconds) { + setTimeout(timer,1000,seconds); } -function timer() { - if ( counter === 0 ) { - document.write("
" + counter); - clearInterval(intervalID); +function timer(seconds) { + if ( seconds > 0 ) { + document.write("
" + seconds + "..."); } else { - document.write("
" + counter + "..."); - counter--; + document.write("
" + seconds); + } + seconds--; + if ( seconds >= 0 ) { + setTimeout(timer,1000,seconds); } } +// 2. Keep track of time without defining any global variables + +// 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("
" + counter); +// clearInterval(intervalID); +// } else { +// document.write("
" + counter + "..."); +// counter--; +// } +// } + countdown(5); From 3475084dc1d6132b0498ae6a6f181c46f898320b Mon Sep 17 00:00:00 2001 From: bluemittens505 Date: Sat, 6 Feb 2016 23:31:45 -0500 Subject: [PATCH 3/4] Level 2 Keep track of time without defining any global variables --- countdown.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/countdown.js b/countdown.js index 6cf462b..42541ea 100644 --- a/countdown.js +++ b/countdown.js @@ -3,24 +3,43 @@ // 3. BONUS: don't define any new variables +// function countdown(seconds) { +// setTimeout(timer,1000,seconds); +// } + +// function timer(seconds) { +// if ( seconds > 0 ) { +// document.write("
" + seconds + "..."); +// } else { +// document.write("
" + seconds); +// } +// seconds--; +// if ( seconds >= 0 ) { +// setTimeout(timer,1000,seconds); +// } +// } + +// 2. Keep track of time without defining any global variables + +var intervalID; + function countdown(seconds) { - setTimeout(timer,1000,seconds); + var counter = { + total : seconds + }; + intervalID = setInterval(timer,1000,counter); } -function timer(seconds) { - if ( seconds > 0 ) { - document.write("
" + seconds + "..."); +function timer(counter) { + if ( counter.total === 0 ) { + document.write("
" + counter.total); + clearInterval(intervalID); } else { - document.write("
" + seconds); - } - seconds--; - if ( seconds >= 0 ) { - setTimeout(timer,1000,seconds); + document.write("
" + counter.total + "..."); + counter.total--; } } -// 2. Keep track of time without defining any global variables - // 1. Use global variable to keep track of time // var counter; From 99d28e8e6c05391543cd6e9d61b21fea0da81878 Mon Sep 17 00:00:00 2001 From: bluemittens505 Date: Sat, 6 Feb 2016 23:42:15 -0500 Subject: [PATCH 4/4] Uncomment last version --- countdown.js | 60 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/countdown.js b/countdown.js index 42541ea..0f7c267 100644 --- a/countdown.js +++ b/countdown.js @@ -3,43 +3,43 @@ // 3. BONUS: don't define any new variables -// function countdown(seconds) { -// setTimeout(timer,1000,seconds); -// } - -// function timer(seconds) { -// if ( seconds > 0 ) { -// document.write("
" + seconds + "..."); -// } else { -// document.write("
" + seconds); -// } -// 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); + setTimeout(timer,1000,seconds); } -function timer(counter) { - if ( counter.total === 0 ) { - document.write("
" + counter.total); - clearInterval(intervalID); +function timer(seconds) { + if ( seconds > 0 ) { + document.write("
" + seconds + "..."); } else { - document.write("
" + counter.total + "..."); - counter.total--; + document.write("
" + seconds); + } + 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("
" + counter.total); +// clearInterval(intervalID); +// } else { +// document.write("
" + counter.total + "..."); +// counter.total--; +// } +// } + // 1. Use global variable to keep track of time // var counter;