Trying to solve time's up in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Trying to solve time's up in JavaScript

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 _______________________________________________________________________________________________ function main() { var seconds = parseInt(readLine(), 10) // Your code here while(4<=1){ documemt.write(1+"<br/>"); i++; } _______________________________________________________________________________________________ it showing unexpected end

11th May 2021, 4:08 PM
Simisola Osinowo
Simisola Osinowo - avatar
25 Answers
+ 5
1. Your while loop is not closed with a curly brace - } 2. The while loop condition is wrong. 4 is not less than or equal to 1, so the code in the while body will not be executed. It should be 4 >= 1. Also in the place of 4, you should use the seconds variable, because 4 is a static value. while (seconds >= 1), or better while (seconds > 0) 3. You have a typo here: documemt.write(), instead document.write() But you must log the result in the console. So, you need to use console.log(), as such: console.log(seconds) 4. You don't have declared 'i' variable, so there is no point to increase i++, since it's not used. Instead you have the seconds variable which you need to decrease on each iteration of the loop. seconds--; In the end the loop should look like this: while (seconds > -1) { console.log(seconds); seconds--; } Or just: while (seconds > -1) console.log(seconds--);
12th May 2021, 1:35 PM
Boris Batinkov
Boris Batinkov - avatar
+ 4
Maybe will be needed setInterval(), then this can be help: https://code.sololearn.com/WXc1pV5DNqPl/?ref=app
11th May 2021, 5:34 PM
JaScript
JaScript - avatar
+ 3
First of all in the while loop you should have a variable for control.
11th May 2021, 4:24 PM
JaScript
JaScript - avatar
+ 2
السلام عليكم
11th May 2021, 7:02 PM
امير الاحزان A.a
امير الاحزان A.a - avatar
+ 2
Boris Batinkov Yaroslav Vernigora Thanks for your help
12th May 2021, 2:33 PM
Simisola Osinowo
Simisola Osinowo - avatar
+ 2
what do you think should be changed?
12th May 2021, 2:36 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
.ym .g. M v
13th May 2021, 3:05 AM
Zechariah YAKAP
Zechariah YAKAP - avatar
+ 1
How?
11th May 2021, 4:52 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Don't know exactly
11th May 2021, 4:56 PM
Simisola Osinowo
Simisola Osinowo - avatar
+ 1
Boris Batinkov Tried this function main() { var seconds = parseInt(readLine(), 10) // Your code here while(seconds>0)console.log(seconds--); } But it counts 5 4 3 2 1 And does not include 0
12th May 2021, 2:35 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
Hi! assign an initial value to the variable "i", then use it in the while loop condition, as well as for output to the screen
11th May 2021, 4:28 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
In this task variable "seconds" used instead of the "i" variable
11th May 2021, 4:32 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Yaroslav Vernigora i have changed the i to seconds still no difference
11th May 2021, 4:39 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
Show your fixed code again
11th May 2021, 4:42 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
function main() { var seconds = parseInt(readLine(), 10) // Your code here while(4<=1){ documemt.write(1+"<br/>"); seconds++; }
11th May 2021, 4:45 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
explain in your own words your condition in the loop. what's in parentheses. what does your condition do?
11th May 2021, 4:46 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Its counts down from 4
11th May 2021, 4:47 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
"it's count down from 4" idk why would you say it counts starting from 4 because you made a typo in document.type syntax! which means it will never print the number and the while loop doesn't have a closed curly bracket so the browser doesn't have a damn clue where to end. you can try doing it. or you can see the code https://code.sololearn.com/WIGJYZNIzS8O/?ref=app
11th May 2021, 5:02 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
1. read the lesson about the while loop again, see the sample code, how it works 2. make a normal working condition of the loop 3. think if you have 4 seconds, and you need to do a countdown, should the seconds increase or decrease? 4. change document.write to console.log here as well
11th May 2021, 5:07 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
the test program does not skip document.write() instead, use console.log()
12th May 2021, 1:48 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar