0

The snail in the well problem

I know I'm doing something really basic wrong but I've been looking at it for so long, I don't see it. It's a simple while loop. The 2 inputs for depth of the well is 42 and 128. If I set my variable day = 0, 42 works fine but, 128 does not. If I set my variable day = 1, 128 works but 42 does not. What am I doing wrong? var depth = 42/128; var prog = 0; var day = 1; while (prog < depth) { prog = (prog + 5); day ++; } console.log (day); }

13th Mar 2021, 12:42 AM
Paul Hudson
Paul Hudson - avatar
7 Réponses
+ 3
you don't have to hardcode input: there will provided for you trough the parseInt(readline(),10) call... function main() { var depth = parseInt(readLine(), 10); //your code goes here var climb = 2, days = 0; do climb -= 2, ++days; while ((climb += 7)<depth); console.log(days); }
13th Mar 2021, 1:04 AM
visph
visph - avatar
0
How can I do both test cases?
4th Sep 2021, 9:51 PM
saeid piryaee
saeid piryaee - avatar
- 1
I have a problem with some entries like 42,32 the result is greater by one day. function main() { var depth = parseInt(readLine(), 10); //your code goes here var d=0; for(i=0;i<=depth;i+=7) { if(i>=depth) break ; i-=2; d++; } console .log (d); }
20th Mar 2021, 8:53 AM
‎‏‪Maram Al_Qadasi‬‏‎
‎‏‪Maram Al_Qadasi‬‏‎ - avatar
- 1
May Any one help my with this code? function main() { var depth = parseInt(readLine(), 10); //your code goes here var d=0; for(i=0;i<=depth;i+=7) { if(i>=depth) break ; i-=2; d++; } console .log (d); }
20th Mar 2021, 8:54 AM
‎‏‪Maram Al_Qadasi‬‏‎
‎‏‪Maram Al_Qadasi‬‏‎ - avatar
- 1
@Maram Al_Qadasi , Good code, just missing one or two bits. See solution https://www.sololearn.com/learning/eom-project/1024/979
2nd Apr 2021, 11:00 AM
Abdalla Bachu
Abdalla Bachu - avatar
- 1
day = 0; while (depth>0) { depth-=7; day++; if (depth>0) {depth+=2;} } console.log(day); }
3rd Apr 2021, 2:49 PM
M Z
- 1
👍
3rd Apr 2021, 4:24 PM
Abdalla Bachu
Abdalla Bachu - avatar