+ 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
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--);
+ 4
Maybe will be needed setInterval(), then this can be help:
https://code.sololearn.com/WXc1pV5DNqPl/?ref=app
+ 3
First of all in the while loop you should have a variable for control.
+ 2
السلام عليكم
+ 2
Boris Batinkov
Yaroslav Vernigora
Thanks for your help
+ 2
what do you think should be changed?
+ 2
.ym .g.
M v
+ 1
How?
+ 1
Don't know exactly
+ 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
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
0
In this task variable "seconds" used instead of the "i" variable
0
Yaroslav Vernigora i have changed the i to seconds still no difference
0
Show your fixed code again
0
function main() {
var seconds = parseInt(readLine(), 10)
// Your code here
while(4<=1){
documemt.write(1+"<br/>");
seconds++;
}
0
explain in your own words your condition in the loop. what's in parentheses. what does your condition do?
0
Its counts down from 4
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
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
0
the test program does not skip document.write() instead, use console.log()