Hi I need some help, I'm a bit lost with this task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hi I need some help, I'm a bit lost with this task

I'm doing this task: The snail goes up 7 feet each day and back 2 feet each night. How many days will it take the snail to climb out of a well with the given depth? Input example: 31 Example 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 climb out of the well during the day, without slipping again that night. Hint: You can use a loop to calculate the distance the snail covers each day, and break the loop when reach the desired distance. I don't know what to do exactly, I don't know if i have to do a loop, but if i have to i don't know how in aspect that wat is going to be the condition, or if the iterations has to be the same as the feet but it doesn't make sense because the task says that the objective is to show the amount of iterations that takes until reach the feets.

14th Dec 2022, 4:15 AM
Alan Restrepo
Alan Restrepo - avatar
30 Answers
+ 5
Alan Restrepo Glad I could help. I did the whole course yesterday, in one day, so it helped me as well, the task made me think😁 Pay it forward when you can, help others. And nice work, you got the job done!
14th Dec 2022, 9:21 AM
Scott D
Scott D - avatar
+ 4
Scott D yeah you right, you could be a teacher 👍
14th Dec 2022, 9:23 AM
Alan Restrepo
Alan Restrepo - avatar
+ 2
Scott D men Crist bless you, it work perfectly, thanks for the patience you explain me every thing very well. Thanks for the help
14th Dec 2022, 9:15 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
Alan Restrepo The loop will have to continue until it's equal to or greater than depth. In the loop, calculate the distance as shown, so create an equation that will take the distance, increase it by 7 and then subtracts 2. You also need a seperate variable, such as days = 0 Everytime the loop iterates, that variable needs to increase by 1 Finally, break the loop when the distance travelled is greater than or equal to the depth. I don't want to give away too much or too little, it's best you give it a shot then come back with an attempt (your code) if it doesn't work. And you can do it steps, like figuring out the loop first, then how to get the count (days) to increase for each iteration, finally setting up the break. That's just one example. And using console.log to monitor things in the loop can be useful, just delete them later. I've found developing the method is often the difficult part, deciding how to apply what you've learned is easier if you can visualize the path to the result.
14th Dec 2022, 4:38 AM
Scott D
Scott D - avatar
0
I know it might be easy but I'm not getting it
14th Dec 2022, 4:15 AM
Alan Restrepo
Alan Restrepo - avatar
0
Scott D I'm going to try to make a code and see what happens, thanks for the hint, I'll be back if i don't see to figure it out
14th Dec 2022, 4:43 AM
Alan Restrepo
Alan Restrepo - avatar
0
14th Dec 2022, 4:45 AM
Scott D
Scott D - avatar
0
Scott D can I make you a question ?
14th Dec 2022, 5:01 AM
Alan Restrepo
Alan Restrepo - avatar
14th Dec 2022, 5:02 AM
Scott D
Scott D - avatar
0
Scott D I'm thinking in what you said, and for an example I know that for increment a variable you have to use the (++) and for the equation I'm thinking to make something like this Var x = 7 - 2; Var y = x + 7 - 2; That would be correct?
14th Dec 2022, 5:08 AM
Alan Restrepo
Alan Restrepo - avatar
0
Alan Restrepo Yes, good. Remember Var is "let" in js. And you can also simplify the equation a little bit instead of using 2 var like x and y. before loop: let distance = 0; In loop: distance += 7 - 2; That is the same as: distance = distance + 7 - 2 So if total distance is 5 after first iteration, the second iteration is: distance = 5 + 7 - 2 then distance = 10 + 7 - 2 and so on.
14th Dec 2022, 5:21 AM
Scott D
Scott D - avatar
0
Scott D and I'm sorry, but the while loop is a good option or a food loop?
14th Dec 2022, 5:23 AM
Alan Restrepo
Alan Restrepo - avatar
0
Alan Restrepo I tested it with a for loop, I find them a little cleaner or more compact, but you could also use a while loop. They are almost the same thing in this case. I think whatever you feel more comfortable with.
14th Dec 2022, 5:26 AM
Scott D
Scott D - avatar
0
Scott D ok I'm going to try to make the code
14th Dec 2022, 5:28 AM
Alan Restrepo
Alan Restrepo - avatar
0
Alan Restrepo Good. And trying to mentally create the strategy like you did is a great first step!
14th Dec 2022, 5:30 AM
Scott D
Scott D - avatar
0
Scott D could you see this code and tell me if I'm in the right direction: let x = 32; let distance = 0; for (; distance>=x, distance += 7 - 2; ) { distance ++ console.log(distance); }
14th Dec 2022, 5:51 AM
Alan Restrepo
Alan Restrepo - avatar
0
Alan Restrepo Geting close. So input is 31, use that for x. also create var for days = 0 That will count iterations (how many days). Then for(let i = 0; i <= x; i++) This will make as many iterations as needed till you break. Inside for statement use { distance equation and days++; } This way, for each loop, distance is 5 more but day only increases by 1. Then make if statement for break: if distance is >= x break Of course you need to use correct syntax. This should also be inside the for loop. Last, consider - should break come before or after days++? That will be easy to test since you know one example of input and output.
14th Dec 2022, 6:09 AM
Scott D
Scott D - avatar
0
Scott D ok I'm going to try it
14th Dec 2022, 6:17 AM
Alan Restrepo
Alan Restrepo - avatar
0
Scott D how about this: let x = 31; let distance = 0; let days = 0; for (let i = 0; i <=x; i++) { distance += 7-2; days++; if (distance >= x){ break; } console.log(days); }
14th Dec 2022, 7:33 AM
Alan Restrepo
Alan Restrepo - avatar
0
Alan Restrepo Did you test it? Very close! There is one last thing, the output. You only want to output the final day. If the console.log is inside the loop it prints each iteration. Move that outside and it should be good.
14th Dec 2022, 8:08 AM
Scott D
Scott D - avatar