+ 3

Can anyone help me to understand this question?

Im a little new here and I've been trying to use JavaScript loops to solve this problem but I just can't seem to get my head around it. Question : 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 Apr 2021, 7:51 AM
Konstantin Taylor
Konstantin Taylor - avatar
37 Answers
+ 7
and the logic is independent of the programming language. if you have the wrong algorithm, you will also solve it incorrectly on python for example. I'm trying three languages: js, python, and ruby. and imagine, the logic is the same! only the syntax of writing the program differs
6th Apr 2021, 8:49 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 4
There is one šŸŒ and it is daily climbs the wall 7 feet up but at night it slips out 2 feet. means it slips from 7 feet so 7-2 = 5. Now you have to figure out how many days it takes to get out from particular given depth. In question input is 31 so 31 is total feet you have. now if šŸŒ is climb 7 feet at day and slip 2 feet at night so in 1 entire day it climb only 5 feet. so 31-5 = 26 feet is still remains. now next day it again climb 7 feet and slips 2 feet so. 1st 5feet + 7feet - 2feet slips =10. Two entire day is completed. and snail is climbed 10 feet. still snail have to climb 21 feet
6th Apr 2021, 8:07 AM
Java Developer
Java Developer - avatar
+ 4
function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = depth / (5); console.log(Math.round(day)); }
7th Apr 2021, 10:35 PM
IbrahimCPS
IbrahimCPS - avatar
+ 4
function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = depth / (5); console.log(Math.round(day)); }
7th Apr 2021, 10:35 PM
IbrahimCPS
IbrahimCPS - avatar
+ 4
Look at my post it's solution and is too easy to understand...
7th Apr 2021, 10:36 PM
IbrahimCPS
IbrahimCPS - avatar
+ 4
Post is closed
7th Apr 2021, 10:36 PM
IbrahimCPS
IbrahimCPS - avatar
+ 4
Discussion is closed...
7th Apr 2021, 10:37 PM
IbrahimCPS
IbrahimCPS - avatar
+ 4
Question is closed...
7th Apr 2021, 10:38 PM
IbrahimCPS
IbrahimCPS - avatar
+ 4
Ask something ELSE!!!
7th Apr 2021, 10:38 PM
IbrahimCPS
IbrahimCPS - avatar
+ 3
function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = depth / (7-2); console.log(Math.round(day)); }
7th Apr 2021, 2:29 PM
IbrahimCPS
IbrahimCPS - avatar
+ 2
Tapabrata Banerjee you solved this task?
6th Apr 2021, 8:06 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
why do you get attached to five instead of seven? that's not how it works there
6th Apr 2021, 8:12 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
With your skills, you can work as an error generator in the code for the testers service šŸ¤­
6th Apr 2021, 9:45 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
The solution is to round off your result.. the code will be: // without slips back at night. var day = depth / (7-2); //to print out put and round the result. console.log(Math.round(day));
7th Apr 2021, 9:55 AM
IbrahimCPS
IbrahimCPS - avatar
+ 1
Hi! Can you show us your code attempt?
6th Apr 2021, 8:00 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
I have not completely solved this problem, but I think that you are suggesting the wrong thing. think about when the snail should come out? and when should I check it out or not?
6th Apr 2021, 8:10 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
In simple input / 5. if it is less than or equal to (<=0) than return that number otherwise add 1 in that
6th Apr 2021, 8:11 AM
Java Developer
Java Developer - avatar
+ 1
we had a heated discussion here, but nothing came from the owner of the question... what does he think about it? welcome to join us!
6th Apr 2021, 8:20 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Tapabrata Banerjee you forgot print command number of days
6th Apr 2021, 8:25 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
and why do you need the "d" - counter variable when the "i" loop variable does a great job of it
6th Apr 2021, 8:27 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar