+ 27

The Snail in the Well ? JS

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. 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.

8th Dec 2020, 5:16 AM
Ibrahim Matar
Ibrahim Matar - avatar
142 Answers
+ 162
function main() { var depth = parseInt(readLine(), 10); //your code goes here i = 0; for (; depth > 0;) { i++; depth -= 7 if (depth > 0) { depth += 2 } } console.log(i); } Here ya go m8.
24th Dec 2020, 7:46 AM
Deniz
Deniz - avatar
+ 81
For those interested, here's an explanation on why Ibrahim Matar's answer works. function main() { var depth = parseInt(readLine(), 10); for(dis = 7, day = 1; dis < depth; dis += 7, day++) { dis -= 2 } console.log(day); } A key part of understanding this lies in knowing the order in which a for loop is evaluated and executed. There are four parts of a for loop (I’ll include the corresponding code from the answer in parentheses for clarity): - initialization (dis = 7, day = 1) - condition (dis < depth) - final-expression (dis += 7, day++) - statement (dis -= 2) Although they are presented in the order listed above, they actually run in this order: 1. initialization (“dis” is declared and given a value of 7, “day” is declared and given a value of 1) 2. condition (if “dis < depth” is true, the for loop will continue onto steps 3 and 4. If it is false, the for loop will stop immediately without executing steps 3 and 4) 3. statement (“dis” is decreased by 2) 4. final-expression (“dis is increased by 7, “day” is increased by 1) From here, the loop will run again, but it starts with step 2 (“dis < depth”), not the first one. This is evaluated, once again looking for either true or false. Going through it step by step. Let’s say “depth = 11”. First time through the loop: - dis is set to 7, day is set to 1 - dis (which is 7) < depth (which is 11) = true (the for loop continues to steps 3 and 4) - dis (which is 7) is decreased by 2, so dis is now 5 - dis (which is 5) is increased by 7, so dis is now 12; and day (which is 1) is increased by 1, so day is now 2 Second time through the loop: - dis (which is 12) < depth (which is 11) = false (the for loop does not continue to steps 3 and 4) - the for loop ends, console.log(day) is executed, and at this moment, day has a value of 2 With more space this could be explained further, but this is why extra conditionals, variables, while loops, and arrays aren't necessary here.
17th Mar 2021, 10:50 PM
Justin
Justin - avatar
+ 48
My answer is here. function main() { var depth = parseInt(readLine(), 10); //your code goes here var climb = 7; var slip = 2; var day = 0; for(workdone=0;workdone<=depth;) { day = day + 1; workdone=workdone+climb; if(workdone>=depth){ break; } workdone = workdone - slip; } console.log(day); }
8th Jan 2021, 6:32 PM
Nga Pyi
Nga Pyi - avatar
+ 38
console.log(Math.round(depth / 5))
16th Feb 2021, 1:04 PM
Shak Daniel
Shak Daniel - avatar
+ 33
My noob code for(x=1; depth >0; x++){ depth -=7; if(depth<=0){ console.log(x); break; } depth +=2; if(depth<=0){ console.log(x); break; } }
15th Dec 2020, 7:38 PM
Vlad
Vlad - avatar
+ 15
So I wasted a lot of time with the loop method, I decided to find a way to simplify the problem by exploring other options and this is what I came up with. function main() { var depth = parseInt(readLine(), 10); day = depth/5; console.log(Math.round(day)); } We know that the snail moves 7 and slides back 2 each night; so that is 5 per day. We know depth is determined by user input. So we divide depth by 5 and we get a number and decimal. Using the Math.round() function we eliminate our decimal and store the answer in the var day. Hope this helps.
22nd Jun 2021, 4:43 PM
Josh Williams
Josh Williams - avatar
+ 12
function main() { var depth = parseInt(readLine(), 10); //your code goes here for (dis=7,day=1;dis<depth;dis+=7,day++){ dis=dis-2; } console.log (day) }
8th Dec 2020, 5:17 AM
Ibrahim Matar
Ibrahim Matar - avatar
+ 9
var climb = 7; var slip = 2; var day = 0; for(workdone=0; workdone=depth;) { day = day + 1; workdone=workdone + climb; if(workdone = depth){ break; } workdone = workdone - slip; } console.log(day); }
9th Jan 2021, 7:29 PM
Grigor
Grigor - avatar
+ 6
function main() { var depth = parseInt(readLine(), 10); //equals 42 in this example var x = 0; //milesDone var y = 0; //dayCount for(i=0 ; i < depth ;i ++){ if (x<depth) { x += 7; if (depth > x) { x -= 2; } y++ } else { console.log(y) break; } } }
20th Jan 2021, 1:48 PM
Nami
Nami - avatar
+ 5
----------------------------------- PASSED ALL TEST CASES ----------------------------------- // VARIABLES const climb = 7; const slip = -2; let day = 0; // SOLUTION for (progress = 0; progress <= depth;) { progress += climb; day +=1; if (progress >= depth) { break; } else { progress += slip; } } // OUTPUT console.log(day);
28th Feb 2021, 9:31 PM
James Norberto
James Norberto - avatar
+ 3
thats my code: function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 0; var way = 0; while (way < depth) { way += 7; day++; if (way >= depth) { break; } way -= 2; } console.log(day); }
25th Mar 2021, 10:07 AM
Passage
Passage - avatar
+ 3
function main() { var profundidade = parseInt(readLine(), 10); // seu código vai aqui, rsrs exclusivo pra Br for (i = 0; profundidade > 0; i++) { profundidade -= 7 if (profundidade > 0) { profundidade += 2 } } console.log(i); }
8th Nov 2021, 8:59 PM
Kiyoshi G Yoshida 🇧🇷
Kiyoshi G Yoshida 🇧🇷 - avatar
+ 2
function main() { var depth = parseInt(readLine(), 10); //your code goes here let data = []; /** Array contains the distance snails moves per days after an incremental +7 and decreamental -2 */ for (let i = 7; depth > 0 && i < depth + 7; i += 7) { if (i < depth) i -= 2; /**it only subtracts 2 as far my incremental value of +7 is less than or equal to input value */ data.push(i); }; console.log(data.length); /**this will output the length of data which will be the number of days */ } or function main() { var depth = parseInt(readLine(), 10); //your code goes here for ( i = 7, day = 1; i < depth; i += 7, day++) { if (i < depth) i -= 2; /**it only subtracts 2 as far my incremental value of +7 is less than or equal to input value */ }; console.log(day); /**this will output the length of data which will be the number of days */ }
10th Jan 2021, 12:20 PM
Ayoola Paul oluwamayowa
Ayoola Paul oluwamayowa - avatar
+ 2
function main() { var depth = parseInt(readLine(), 10); //your code goes here i = 0; for (; depth > 0;) { i++; depth -= 7 if (depth > 0) { depth += 2 } } console.log(i); }
8th Feb 2021, 10:18 AM
Md Momin Uddin Hridoy
Md Momin Uddin Hridoy - avatar
+ 2
function main() { var depth = parseInt(readLine(), 10); //your code goes here var sum = 0; var i=1; while (i>0) { sum += 7; if (sum>=depth) break; else sum -= 2; i++; } console.log(i); }
31st Dec 2022, 8:28 AM
Ogün Orta
Ogün Orta - avatar
+ 2
function main() { var depth = parseInt(readLine(), 10); //your code goes here //distance = distance covered var distance=0; var days=1; while(distance<depth) { distance += 7; if(distance<depth) { distance-=2; days++; } } console.log(days); }
23rd Jan 2023, 11:15 AM
Manvir Singh
Manvir Singh - avatar
+ 1
But why ? Initial 1 day/7 feet not 0 ?
8th Dec 2020, 5:18 AM
Ibrahim Matar
Ibrahim Matar - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here var day=parseInt(0); var ng=parseInt(2); var clday=parseInt(7); for(days=0;days<=depth;){ day=day+1; days=days+clday; if(days>=depth){ break; } days=days-ng; } console.log(day); } hooora!!!
14th Jan 2021, 6:05 PM
Kelvyn Eleazar De Oleo Taveras
Kelvyn Eleazar De Oleo Taveras - avatar
+ 1
function main() { var depth = parseInt(readLine(), 10); //your code goes here if(depth%5<=2) console.log(parseInt(depth/5)); else console.log(parseInt(depth/5)+1); }
21st Jan 2021, 9:31 AM
Vô Phan
Vô Phan - avatar
+ 1
climb =0 for(day=1;climb <depth;day++){ if (climb <depth){ climb =climb +7; if (climb >=depth){ document.write(day); } else{ climb =climb -2; } } }
4th Mar 2021, 7:32 AM
Moneesh Kumar L
Moneesh Kumar L - avatar