The Snail in the Well | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

The Snail in the Well

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. https://code.sololearn.com/cnN2pt00BhvP/?ref=app

15th Oct 2021, 1:06 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
13 Respuestas
16th Oct 2021, 3:54 AM
Rupali Haldiya
Rupali Haldiya - avatar
+ 2
Please post your code here, so we can help you with your code.
15th Oct 2021, 4:05 AM
Rupali Haldiya
Rupali Haldiya - avatar
+ 2
Tahiti🍷 you're welcome 😄 Happy Coding
17th Oct 2021, 1:23 AM
Rupali Haldiya
Rupali Haldiya - avatar
+ 1
Tahiti🍷 Answering... 1. Sum += 7-2 is simply stands for sum = sum + 7 - 2. And == is called comparison operator and here we no longer need to compare these values. 2. document.write() as the name says it will help to write text in document/html, and it is almost ignored when we're working with JS/NodeJs. We simply use a browser window called console to see what is happening with our code instead of document.write(). 3. I mean don't call function when you're submitting your code main () // function call Solo learn will automatically handle this, you've no longer need to call a function by your self. 4. Yes I'm using do while loop (as you were trying to use) 5. No! We don't need that, bcz we are not working with html and we don't need any line break anywhere else in our code. Hope you got it. Thanks.
16th Oct 2021, 6:53 PM
Rupali Haldiya
Rupali Haldiya - avatar
+ 1
It's also comparison operator but it's strict. 2 == "2" //true 2 === "2" //false
17th Oct 2021, 5:12 PM
Rupali Haldiya
Rupali Haldiya - avatar
+ 1
I think I understand it now. Thank you, Rupali
17th Oct 2021, 7:42 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var i; var k=0; for(i=0; i<=depth; i+=7){ k+=1; if(i >= (depth-7) ){ break; } i-=2; }console.log(k); }
30th Oct 2021, 9:29 AM
techDebounce
techDebounce - avatar
0
I have tried the for loop, the while loop, the do while loop. I just don’t know the answer. Please help. 😩
15th Oct 2021, 1:08 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Store the snail's movement in a variable. Take initial value 0 and then increment +7 each day ( you have to take another value to count day ofc ) then decrement -2 from snail's movement if it slips back. let snail = 0 let day = 0 while(true) { }
15th Oct 2021, 2:02 AM
zexu knub
zexu knub - avatar
0
Rupali I thought I had inserted the code. It’s there now. Thank you.
16th Oct 2021, 12:23 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
/*Thank you for helping Rupali. I am new to JS, so please bear with me. I understand your code mostly, but I have some questions. 1. Why did you code sum+= 7-2 instead of sum == 7-2?*/ do{ sum += 7-2; i++; } while (sum <= depth+2); console.log(i) /*2. Why console.log instead of document.write? */ //calling function, but dont call when youre submitting code in the challenge sololearn will automatically handle that. main() /* 3. I’m not sure what you mean by this. Do you mean that I shouldn’t begin my answer with “function main ()”? 4. The challenge also said: 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. I noticed you didn’t use <br/> anywhere in your code. Would it simply be one of several options, to use <br/>? Thank you again. */
16th Oct 2021, 1:40 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Thank you again Rupali . After I asked the question, the next lesson I had in the Learn area for JavaScript was about calling functions, incidentally. 😊
16th Oct 2021, 7:02 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
One more question Rupali Why not sum ===7-2? == is comparison. Isn’t === the “equals” symbol?
17th Oct 2021, 5:02 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar