+ 1

THE SNAIL IN THE WELL

Hello everyone, I am completing the javascript course, however in the second module they pose the following problem, which I have been trying to solve since yesterday, if someone could guide me I would appreciate it very much. Pd: I do not speak English, I am using a translator due to the demand of American users The snail climbs 7 feet each day and retreats 2 feet each night. How many days will it take for the snail to get out of a well with the given depth? Input example: 31 Output example: 6 Explanation: Let's break down the distance that 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 come out of the well during the day, without slipping again that night.

13th Jan 2021, 9:52 PM
Belkin !!
Belkin !! - avatar
8 Réponses
- 1
if((31-(31%(7-2))+2)>31){ console.log(Math.floor(31/(7-2))) } else { console.log(Math.floor(31/(7-2))+1) } should work fine . If it did and you need an explanation of what I did I would add that as well .
13th Jan 2021, 11:03 PM
Abhay
Abhay - avatar
+ 2
Belkin !! Wasn't 6 the answer? I just gave an idea or say a solution for the problem not full code for it ,replace those values with input variables.
14th Jan 2021, 12:51 AM
Abhay
Abhay - avatar
+ 1
Abhay Oh I get it, thank you very much, hey, one last thing would be too much to ask you to explain the example you gave me, please
14th Jan 2021, 12:57 AM
Belkin !!
Belkin !! - avatar
+ 1
Belkin !! Since snail is climbing only 5(7-2) feet in a full day ,you can simply divide the depth of well by 5 feet and you would know how many full days it took the snail to come out of well or is close to it like 31/5= 6 days .( If you round it off to a value lower than actual value(6.2)), Now to confirm that 6 days is enough or not What I did was to get the remainder of depth left after climbing for a full day and subtract that remainder from actual well depth to get actual depth that snail was able to climb i.e. 31-31%5=30, Now I added the height from the last day night since the snail didn't retreated that night and so it was snail climbing 5+5+5+5+5+7 i.e.30+2=32 , And now we know that snail was able to climb 32 feet after 6 days but to confirm it, we check if 32 was greater than actual well depth well to see if it climbed fully ,if not we will add 1 day more to it like what I did . Hopefully you get it ,if not let me know what you didn't understood specifically in my answer .
14th Jan 2021, 10:15 AM
Abhay
Abhay - avatar
0
The result always gives 6, it does not work, thank you very much brother
14th Jan 2021, 12:47 AM
Belkin !!
Belkin !! - avatar
0
Belkin !! I will do that tomorrow ,now it's time for me to sleep .
14th Jan 2021, 1:03 AM
Abhay
Abhay - avatar
0
Abhay Give him brother, no problems, thank you very much
14th Jan 2021, 1:05 AM
Belkin !!
Belkin !! - avatar