+ 1

Save me from going insane

Hey, I am doing the Snail in the Well task, but I cannot solve it somehow. I am literall staring my code for an hour and I can’t get around it. I can’t belive how stupid I am. Please someone explain to me whats wrong with my code before this snale drives me crazy. function main() { var depth = parseInt(readLine(), 10); var days=0; //your code goes here for (i=0;i<=depth;i=i+7){ if(i<depth){i-=2} days++; } console.log(days); }

7th Dec 2021, 4:29 PM
Roland SzabĂł
5 RĂ©ponses
0
I guess you forgot to close the main function. What does the console say though?
7th Dec 2021, 4:36 PM
cadbrooke
cadbrooke - avatar
0
I did close it I just copied it badly. I have edited my text. Half of the cases are good. If the input is 128 the output is 26. Check On the other hand if the input is 42, my output is 9 instead of 8.
7th Dec 2021, 5:09 PM
Roland SzabĂł
0
Your code decrements the depth immediately after the loop action without assessing the height of the snail. Also, your first day, the snail climbs 0 Sequence: Snail climbs 7 day ++ check progress vs depth if progress > depth -> break else depth -2 I think a while loop would be better in this scenario
7th Dec 2021, 5:54 PM
Rik Wittkopp
Rik Wittkopp - avatar
0
I am not sure I understand you, maybe I don’t quite get how the for method works. Yea it does start with 0 but after the 2nd parameter is true it should increment the value by 7, right? So after day 1 its 7 (5 to be more specific, after the if method)
7th Dec 2021, 6:05 PM
Roland SzabĂł
0
I have also tried with while loop(didnt work): function main() { var depth = parseInt(readLine(), 10); var days=0; var i=0; /* //your code goes here for (i=0;i<=depth;i=i+7){ if(i<depth){i-=2} days++; } console.log(days); } */ while (i<=depth){ i+=7; days++; if(i<depth){i-=2} } console.log(days); }
7th Dec 2021, 6:15 PM
Roland SzabĂł