0

Help please!!!

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.

5th Apr 2021, 5:46 PM
Wissam Al monaged
Wissam Al monaged - avatar
12 Answers
+ 5
var depth=31; //make this depth to be get from user var climb=7; var back=2; var days=0; var length=0; while(length<depth){ length=length+climb; // climbed 7 feet up days=days+1; // one day over if(length>=depth) break; length=length-back; // climbing 2 feet back } document.write(days);
7th Apr 2021, 4:37 PM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 5
What is your question?
5th Apr 2021, 5:56 PM
Nico Ruder
Nico Ruder - avatar
+ 4
Hi! Please, show us your attempt!
5th Apr 2021, 6:04 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
Please post your attempt
5th Apr 2021, 6:05 PM
Atul [Inactive]
+ 3
Not necessarily you can do it without loops too
5th Apr 2021, 6:17 PM
Atul [Inactive]
+ 2
Your code is coping the depth covered but it should print the days
5th Apr 2021, 6:15 PM
Atul [Inactive]
+ 2
count the number of cycles? to do this, you can use a separate variable and increase it by one in the loop. or use the i variable, which you did
5th Apr 2021, 6:16 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var x= 7-2; for(var i =x;i<=depth;i +=x){ console.log(i); if(i===25){ i=i+2; } } } How to calculate the number of loops???
5th Apr 2021, 6:09 PM
Wissam Al monaged
Wissam Al monaged - avatar
+ 1
How??? Atul
5th Apr 2021, 6:18 PM
Wissam Al monaged
Wissam Al monaged - avatar
5th Apr 2021, 6:19 PM
Wissam Al monaged
Wissam Al monaged - avatar
+ 1
No, you must do it yourself. Follow me: your program is very wrong. think like a snail: you should create a loop and each iteration at the beginning of the loop add 7 feet, then check to see if the snail has come out of the well, if so, exit the loop. if the snail has not yet emerged, it slides two feet and the cycle resumes
5th Apr 2021, 6:22 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Thank you very much Arun Ruban SJ
7th Apr 2021, 4:53 PM
Wissam Al monaged
Wissam Al monaged - avatar