0
The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a wel
ماالخطأ في كودي؟؟ function main() { var depth = parseInt(readLine(), 10); //your code goes here var i=1; while (i<depth) { var x=7; var y=2; var z=x-y; var s=z+(7-2); console.log(s); i++; } }
4 ответов
+ 2
Declearing variables inside this loop and assigning fixed values will generate the same output, no matter how long the loop.
First don't decleare variables inside the loop, you need two variables, one for days and the other for the distance.
+ 1
I suggest you to practice making patterns, because they use loops, for example:
*
* *
* * *
* * * *
You can build like these structures using loops.
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var distance=0, day=1;
while(distance<depth){
distance+=7;
if(distance<depth)
{distance-=2;
day++;}
}
console.log(day);
}
+ 1
Thank you, hopefully I will practice
0
True, but I tried declaring the variables outside the loop but still the same error, can you write me the correct solution please?