+ 6

“Snail in the well” help (JS)

Hi All, I am struggling a bit with this challenge, could someone help explain how to work this? After working on a few possibilities I am stuck because I am unsure how to separate the days into two for day and night for “+7” and “-2”. This one definitely has me stumped. Thank you for any help! 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.

12th Jan 2021, 4:42 PM
KfMand08
KfMand08 - avatar
32 Answers
+ 17
Thank you for your help!! Is “let” similar to “var”? I haven’t used that yet. I went back and forth through various loop options and ended with what you had used; “while.” I was confused by the “while (true)” part though because what part are you saying while it‘s true? I put in “whille(climb<depth)” to do the below, which it accepted but I just want to be sure the way I did it was accurate moving forward? function main() { var depth = parseInt(readLine(), 10); //your code goes here var climb = 0; var days = 0; while (climb < depth) { days++; climb += 7; if (climb >= depth){ break; } climb -= 2; } console.log(days); }
12th Jan 2021, 5:21 PM
KfMand08
KfMand08 - avatar
+ 7
function main() { var depth = parseInt(readLine(), 10); //your code goes here let climbedDistance = 0; for(days = 1; depth > climbedDistance; days++) { climbedDistance += 7; if(climbedDistance >= depth) console.log(days); else climbedDistance -= 2; } } So we just need to compare the distance the snail climbed and the depth of the well. We start with the day 1, increment the distance by 7 and start our comparison. In every iteration we check the condition (climbedDistance >= depth) until it becomes true and then log the accumulated number of days on the console. Easy, right?
5th Sep 2021, 6:17 PM
Pouria Hosseini
Pouria Hosseini - avatar
+ 6
function main(){ var depth = parseInt(readLine(),10); var days = 0; var feet = 0; while(feet<depth){ ++days; feet +=7; if(feet > depth -1){ break; feet -=2; } console.log(days); }
25th Apr 2021, 8:36 AM
githinji bonface
githinji bonface - avatar
+ 5
There's a pretty simple solution for this problem. On average the snake will climb 5 feet per day and will climb a maximum of 7 which is 2 more than 5. This means we can subtract 2 from the distance and then divide by 5 to get the amount of days: (32 - 2) / 5 = 6 If you need to round you have to use Math.ceil() because for example 6.1 days don't work but instead you need whole 7 days to reach the distance.
12th Jan 2021, 4:48 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 5
let day = 1; let climb = 7; let depth = 32; for (; climb < depth; climb +=7) { day++; if (climb === depth) {break; } else { climb = (climb - 2); } }; console.log(day);
8th Jun 2021, 2:01 PM
Fakhriddin Mukhtarov
Fakhriddin Mukhtarov - avatar
+ 5
// easy solution function main() { var depth = parseInt(readLine(), 10); //your code goes here var go = 7-2 var dist = 0 var day = 0 for(let go =5; dist<=depth-3; dist +=go){ day+=1 } console.log(day) }
8th Jun 2022, 10:15 AM
Ibrahim Aminu
Ibrahim Aminu - avatar
+ 3
function main() { var depth = parseInt(readLine(), 10); //tu cĂłdigo va aquĂ­ var d = 0; // dĂ­as var c = 0; // caracol while (c < depth) { c += 7 - 2; d += 1; if (c > depth) { if (c - 2 <= depth) { console.log(d); break; } else if (c > depth) { console.log(d - 1); break; } } } }
6th Jun 2021, 6:40 PM
Kevin Alexander Alvarez Echeverri
Kevin Alexander Alvarez Echeverri - avatar
+ 3
var day = Math.ceil((depth-2)/5); Thats it!! Just output it....Thanks Aaron Eberhardt
14th Aug 2021, 7:29 AM
Piyush A. Mulatkar
Piyush A. Mulatkar - avatar
+ 2
var x = 0; var day; do{ x+=1; day = Math.ceil((depth -2) /5); }while(depth<=x); // or (depth < x); console.log(day);
1st Jul 2021, 1:11 PM
Pavle
+ 2
function main() { var depth = parseInt(readLine(), 10); var climb = 0; var days = 0; for (; climb < depth; days++){ climb += 7; if(climb >= depth){ }else{ climb -=2; } } console.log(days); }
30th Sep 2021, 3:03 PM
Clark Jr. Berja Canlog
Clark Jr. Berja Canlog - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here let day = 1; for (climb = 7; climb < depth; climb +=5) { day++; while (climb === depth) { break; } } console.log(day); }
8th Aug 2021, 9:46 PM
Adeoye Paul
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var days = 1; for (climb=7; climb<depth; climb+=7) { climb-=2; if (climb==depth){ break; console.log(days); } else days++ } console.log(days); }
24th Mar 2022, 4:03 PM
Ver Savilla Sangil
Ver Savilla Sangil - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var climb = 7; var slips = 2; var distance = 0; var days = 0; while(distance < depth) { if(distance >= depth - climb) { distance = distance + climb; days++; console.log(days); break; } else { distance = distance + climb - slips; days++; } } }
25th Aug 2022, 4:42 AM
Yervand Marikyan
Yervand Marikyan - avatar
+ 1
I still don't entirely understand why climb -= 2 must come after the break; Can someone please explain?
25th Sep 2022, 8:24 PM
Anthony Black
Anthony Black - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var snail = 0, day = 0; while((snail + 2) < depth){ snail = snail+7-2; day++; } console.log(day); }
19th Jan 2023, 11:25 AM
Belay Birhanu
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var feet = 0; for (i=1;;i++){ feet += 7; if (feet >= depth) break; feet -= 2; } console.log(i) }
10th Feb 2023, 11:38 AM
Sudharshan J
Sudharshan J - avatar
0
Plz anyone can explain me that plz
28th Apr 2021, 7:30 AM
Aurosish dev
Aurosish dev - avatar
0
Explanation :- 1.) 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. OR 2.) On average the snake will climb 5 feet per day and will climb a maximum of 7 which is 2 more than 5. This means we can subtract 2 from the distance and then divide by 5 to get the amount of days: (32 - 2) / 5 = 6 If you need to round you have to use Math.ceil() because for example 6.1 days don't work but instead you need whole 7 days to reach the distance.
7th Jun 2021, 12:41 PM
RAJAT AGRAWAL
RAJAT AGRAWAL - avatar
0
function main() { var depth = parseInt(readLine(), 10); //your code goes here var days = 0; for (i=0; i<depth; i++){ // on average it takes 5 days to climb. we get the absolute value of the division between the depth and 5 days = ~~(depth / 5); // if the remainder of the division is greater than 2 then the snail will slip back thus we add an extra day if((depth % 5) >2){ days +=1 } } console.log(days) }
8th Sep 2021, 11:17 AM
wesley ochieng
wesley ochieng - avatar
0
function main() { var depth = parseInt(readLine(), 10); //tu cĂłdigo va aquĂ­ var d = 0; // dĂ­as var c = 0; // caracol while (c < depth) { c += 7 - 2; d += 1; if (c > depth) { if (c - 2 <= depth) { console.log(d); break; } else if (c > depth) { console.log(d - 1); break; } } } }
12th Dec 2021, 3:14 AM
MN FATHIMA RIHAM - s92064451 -321424451
MN FATHIMA RIHAM - s92064451 -321424451 - avatar