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++; } }

24th Jan 2022, 1:45 PM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar
4 Answers
+ 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.
24th Jan 2022, 3:13 PM
Mohammed
+ 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); }
24th Jan 2022, 4:09 PM
Mohammed
+ 1
Thank you, hopefully I will practice
24th Jan 2022, 4:20 PM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar
0
True, but I tried declaring the variables outside the loop but still the same error, can you write me the correct solution please?
24th Jan 2022, 4:01 PM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar