Loop and break
Hey fellow learners. I have a question regarding the exercise solution below. It is correct but I don't fully grab the logic behind it. At the end of the exercise the "+=2" part confuses me. What makes it possible that "depth" stops at the right moment and not exeeding "0" again to restart the whole loop? It can't be the "break" right? Because that statement came before the "+="? 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 re function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 0; for (;depth>0;) {depth -=7; day ++; if(depth <= 0) break; depth+=2;} }