JS Time's Up! Challenge Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JS Time's Up! Challenge Question

I'm having some trouble understanding the while loop, specifically with the JS Time's Up! challenge question: Write a program-timer, that will take the count of seconds as input and output to the console all the seconds until timer stops. Sample Input 4 Sample Output 4 3 2 1 0 My code: function main() { var seconds = parseInt(readLine(), 10) // Your code here while(seconds>=0); document.write(seconds + '<br />'); seconds--; } What am I missing?

20th Apr 2021, 10:13 PM
Cameron
9 Answers
+ 2
Cameron Please remove the semicolon on this line. while(seconds >=0); { Your while while loop should just look like this... while(seconds>=0) { console.log(seconds); seconds--; }
21st Apr 2021, 10:07 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 2
It's just a syntax issue. Your while loop needs curly braces. while(seconds>=0) { console.log(seconds); seconds--; } Also, you want to console.log(seconds) instead of writing to the document. You won't need a line break if you use console.log.
20th Apr 2021, 10:35 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 1
function main() { var seconds = parseInt(readLine(), 10) // Your code here while(seconds>=0); { console.log(seconds); seconds--; } }
7th Jul 2022, 7:26 PM
Khabibullin Ismagil
Khabibullin Ismagil - avatar
+ 1
This Correct function main() { var seconds = parseInt(readLine(), 10) // Your code here while(seconds>=0){ console.log(seconds) seconds-- } }
16th Jul 2022, 10:33 AM
Gian Dumadaug
Gian Dumadaug - avatar
0
function main() { var seconds = parseInt(readLine(), 10) // Your code here while(seconds>=0); { console.log(seconds); seconds--; } } Outputs: Execution Timed Out! I've tried every iteration I could think of...just lost at this point.
21st Apr 2021, 7:05 PM
Cameron
0
Thanks, mate!
22nd Apr 2021, 12:13 AM
Cameron
0
No worries 🙂
22nd Apr 2021, 12:33 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
0
function main() { var seconds = parseInt(readLine(), 10) // my code while (seconds >= 0) { console.log(seconds); seconds--; } } Good Luck
25th Jan 2022, 8:16 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
Pls 🙏🙏 I'm trying too hard to solve python times up test but still my output is 3 2 1 Instead of 3 2 1 0 I'm putting my code as 👇 # take the number as input number = int(input()) #use a while loop for the countdown while number > 0: print(number) number = number - 1
14th Nov 2023, 8:42 AM
Niku The Warrior
Niku The Warrior - avatar