+ 2

What wrong in the code javascript project snail in the well

function main() { var depth = parseInt(readLine(), 10); //your code goes here var day=0; var climbsDay=7; var slipsDay=2; var i=0; while(depth>0){ if(depth>=i){ i=i+climbsDay; i=i-slipsDay; day++; }else{ break; } } console.log(day) }

31st Jul 2022, 8:49 PM
Walid
Walid - avatar
3 RĂ©ponses
+ 3
You didnt include next case, if snail climb to top during daytime, we dont need to wait night time for snail to go back, but in your code you do this, thats why 2 test cases fails. One little tip for while loop, never use variable you dont change value, in your while loop you type while(depth > 0) { // your code } But you never change depth value, so if you dont add break, as you did you will make infinity loop, you should compare current snail position (in your case variable i) and depth - final goal. while(i < depth) { // your code } Your while loop works but it dont follow best practices, so I didnt changed this. https://code.sololearn.com/W8CW4Mf4b4V5/?ref=app
31st Jul 2022, 11:36 PM
PanicS
PanicS - avatar
+ 2
thanks for the help
1st Aug 2022, 3:21 PM
Walid
Walid - avatar
+ 1
when i submitted the code work for 4 of 5
31st Jul 2022, 8:51 PM
Walid
Walid - avatar