I'm unable to solve this... Pls help me!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I'm unable to solve this... Pls help me!!

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.

6th Oct 2021, 10:29 AM
Annesh Lobdha Singh
Annesh Lobdha Singh - avatar
5 Answers
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here let d = 0; let day = 0 while(true) { d += 7 day++ if(d >= depth) break; else d-=2; } console.log(day) }
6th Oct 2021, 10:30 AM
zexu knub
zexu knub - avatar
+ 2
Lothar Yes, sorry i understand & i agree with you. 😅 I'll keep that in mind. I surely don't want someone ruin the fun from me by giving the solution straight so, i shall not do that either.
6th Oct 2021, 10:47 AM
zexu knub
zexu knub - avatar
+ 2
Ancient Coder well as you can see we have 2 variable d & day where d counts as the snail movements & day just simply log the days it took. With the d variable we increment 7feet each day & decrement 2feet for the night unless the snail got out of the well which we do by simply putting the if condition. Hope you understood Happy Learning ;)
6th Oct 2021, 10:55 AM
zexu knub
zexu knub - avatar
+ 1
Yep... I need explanation i guess!
6th Oct 2021, 10:35 AM
Annesh Lobdha Singh
Annesh Lobdha Singh - avatar
0
Let me know if you need the explanation as well ;)
6th Oct 2021, 10:32 AM
zexu knub
zexu knub - avatar