Snail In the Well | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Snail In the Well

Can some explain this code to me it's a bit confusing :/ function main() { var depth = parseInt(readLine(), 10); //your code goes here i = 0; for (; depth > 0;) { i++; depth -= 7 if (depth > 0) { depth += 2 } } console.log(i); } Question : 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 well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night. Hint: You can use a loop to calculate the distance the snail covers each day, and break the loop when it reaches the desired distance.

10th Nov 2022, 10:54 AM
Liyutsa Zirange
Liyutsa Zirange - avatar
4 Answers
+ 3
Explanation is already there with an example..! Where you have confusion?
10th Nov 2022, 7:05 PM
Jayakrishna 🇮🇳
+ 1
Actually when Sololearn asked me this question i didn't know at first how to answer it - you can say that i didn't get the questions that was confusing and then when i saw it's solution I don't know how that solution came so that's my confusion :)
11th Nov 2022, 10:52 AM
Liyutsa Zirange
Liyutsa Zirange - avatar
+ 1
Liyutsa Zirange to study how the code works, pretend you are the computer running the program. Write down the two variable names and keep track of their values as you execute each statement. You will begin to see how it models the problem and finds the answer.
11th Nov 2022, 2:06 PM
Brian
Brian - avatar
+ 1
Liyutsa Zirange Still, if I have to answer, seems I can only copy paste explanation. To understand, as @Brian said, "The explanation is about sample input 31. So same way you take down steps for input 31 and variables values changings.. Compare with Task explanation. 5,10,15,.. there are steps covered by snail after the day.. The steps taken down are "Tells how the solution came only.."
11th Nov 2022, 2:30 PM
Jayakrishna 🇮🇳