Snail in the well | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Snail in the well

Hi, I've seen a lot of answers on this and I mostly understand them, but they weren't my first thought. I appreciate that I'm sure my method is over complicated but because it's the way I thought of it first on my own id like to understand where I went wrong. If you're unfamiliar, there's a snail in a well of random depths in inches, it climbs 7 Inches each day and falls 2 inches every night, so I need to code something that figured out how many days it will take and I don't understand what's wrong with what I put. function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 0 for (;depth>=0;){ day++ depth+7 if (depth<=0){ break; } else { depth-2 } } console.log(day) }

1st Feb 2021, 9:49 PM
Adamjhw
Adamjhw - avatar
7 Answers
+ 3
I think you meant to do this , var day = 0 for (;depth>=0;){ day++ depth=depth-7 if (depth<=0){ break; } else { depth=depth+2 } } console.log(day) } And it's a really simple and nice way to do it ,I remember answering someone else with a solution for it where I just overcomplicated the code .
1st Feb 2021, 10:18 PM
Abhay
Abhay - avatar
+ 2
Thank you so much, I thougt I fixed the + and - being the wrong way around, but I completely forgot about saying "depth = depth +7", it was driving me crazy, thank you
1st Feb 2021, 10:42 PM
Adamjhw
Adamjhw - avatar
+ 2
// the best 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
+ 1
try this solution to the snail in the well problem when it hepls then you vote function main() { var depth = parseInt(readLine(), 10); //your code goes here var climb = 7 var slip = 2 var days = 1 for (workdone = climb ;workdone < depth;days = days+1 ) { if (workdone==depth) { break; } workdone = workdone + climb; workdone = workdone - slip; } console.log(days) }
5th Feb 2021, 3:07 PM
Richard Ansong Somuah
Richard Ansong Somuah - avatar
+ 1
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:38 PM
Kevin Alexander Alvarez Echeverri
Kevin Alexander Alvarez Echeverri - avatar
0
Checked all the answers around here and concluded this will be the shortest code possible: function main() { var depth = parseInt(readLine(), 10); var day = 0 for (;depth>0;){ depth-=7 day++ if (depth<=0) break; depth+=2} console.log(day)}
4th Feb 2021, 4:23 PM
Who Cares
0
With just one if statment , think out of the box , you dont need for loop at all : if (depth%5<3){ var days=depth/5; }else{ var days=(depth/5)+1; } console.log(parseInt(days));
4th Dec 2021, 6:12 AM
Murtada abed
Murtada abed - avatar