0

Please help me solve this in JavaScript

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.

26th Jun 2022, 5:00 PM
Aliyu Ahmad
Aliyu Ahmad - avatar
3 Answers
0
Look at the explanation and try to understand the outcome. Hint: You can solve it using a loop. The snail starts down the well and climbs up and slides down until it is out of the well.
26th Jun 2022, 5:29 PM
Lisa
Lisa - avatar
0
var dis=31; const climb=7; const slip=2; var val=0; var day=1; while (val<dis){ val += climb; if (val>dis){ break; }else{ val -= slip; } day++; } console.log(day); This should work.
26th Jun 2022, 6:07 PM
Satyam Mishra
Satyam Mishra - avatar
0
Thanks it works
26th Jun 2022, 6:22 PM
Aliyu Ahmad
Aliyu Ahmad - avatar