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
13 RĂ©ponses
+ 3
Tahitiđ· Here you go
https://code.sololearn.com/cOKajIXcrz42/?ref=app
Happy Learning.
+ 2
Please post your code here, so we can help you with your code.
+ 2
Tahitiđ· you're welcome đ
Happy Coding
+ 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.
+ 1
It's also comparison operator but it's strict.
2 == "2" //true
2 === "2" //false
+ 1
I think I understand it now. Thank you, Rupali
+ 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);
}
0
I have tried the for loop, the while loop, the do while loop. I just donât know the answer. Please help. đ©
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) {
}
0
Rupali I thought I had inserted the code. Itâs there now. Thank you.
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. */
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. đ
0
One more question Rupali
Why not sum ===7-2?
== is comparison.
Isnât === the âequalsâ symbol?