+ 2

JS- The Snail in the Well

Need help with this project, will sb guide 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.

9th Aug 2022, 3:59 AM
Bahar Hasanova
Bahar Hasanova - avatar
2 Réponses
+ 2
function main() { var depth = parseInt(readLine(), 10); //your code goes here var i = 0; var result = 0; while(i<depth){ i+=7; result+=1; if(i>=depth){ break; } else { i-=2; } } console .log(result ); }
10th Aug 2022, 5:07 PM
Jahongir
Jahongir - avatar
0
Thanks!
11th Aug 2022, 10:42 AM
Bahar Hasanova
Bahar Hasanova - avatar