What is the Mistake in the code? It satisfy only 3 test cases only | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the Mistake in the code? It satisfy only 3 test cases only

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. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- function main() { var depth = parseInt(readLine(), 10); //your code goes here var claim_distance=7; var slip_distance=2; var distance_covered=0; for(day=1;day<=depth/5;day++) { if(distance_covered >= depth) { break; } else { distance_covered += claim_distance - slip_distance ; } } console.log(day); }

7th Apr 2022, 5:00 AM
Ashok Ghanta
Ashok Ghanta - avatar
1 Answer
+ 1
You over-simplified the problem by dividing 5. The snail moves in this sequence Day 1 0 + 7 = 7 if not out, 7 - 2 = 5 Day 2 5 + 7 = 12 if not out, 12 - 2 = 10 See? The checking should be done immediately after +7 phase of each day.
7th Apr 2022, 5:22 AM
Gordon
Gordon - avatar