Can someone please help me with the snail problem from JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone please help me with the snail problem from JavaScript?

The snail moves 7 feet daily and slides 2 feet nightly. Given a variable depth of a well, log how many days would it take the snail to reach the top? I feel like I'm at least thinking of the problem correctly, but I'm pretty new to coding and am unsure of the formation. Any help would be appreciated. I don't need the answer, just maybe a bit of knowledge I'm missing to get there? Here's the code I have been working with. Thanks in advance! function main() { var depth = parseInt(readLine(), 10); var day = 0 var distance = 0 while (distance < depth){ distance+7-2; ++day } console.log (day); }

4th May 2022, 1:31 AM
Josh Ulm
Josh Ulm - avatar
4 Answers
+ 1
Yes. distance+7-2 it calculates but without storing result , its no use . break; statement can be used to break a loop.. Ex: on a condition: while(..) { //... if ( condition ) break; //.. }
4th May 2022, 12:16 PM
Jayakrishna 🇮🇳
0
store back result for distance.. distance = distance+7-2; But you should not subtract 2 for the last time if distance+7 is reached depth..
4th May 2022, 11:51 AM
Jayakrishna 🇮🇳
0
Thank you for replying! Are you saying that my distance variable should be the distance equation you replied? Distance=distance+7-2 Is there a way to break from the loop in the middle of the equation if need be?
4th May 2022, 12:11 PM
Josh Ulm
Josh Ulm - avatar
0
A side comment: instead of pasting the code, save it in Code Playground and link it in the question. Easier for debugging and helping.
4th May 2022, 6:29 PM
Emerson Prado
Emerson Prado - avatar