0

For-loop stament confusion

Hello, I'm confused as to why the +=7 can't be put inside the initial parantheses () but has to be put inside the brackets {} to to succeed the exercise? 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. function main() { var depth = parseInt(readLine(), 10); //your code goes here day = 0; distance = 0; for(; distance < depth; ) { day ++ distance +=7 if(distance >= depth ) {break } distance -= 2; } console.log(day ); }

15th Feb 2021, 3:33 PM
SuperKitsune94
SuperKitsune94 - avatar
10 Answers
+ 1
Opposite. It's because the distance -= 2 is after the break. It MIGHT NOT happen because the break could happen and end the loop. for(; distance < depth; ) { day++ distance +=7 //7, 12, 17, 22, 27, 32 if(distance >= depth ) {break } distance -= 2 //5, 10, 15, 20, 25 (30 doesn't happen because 32 >= depth and break.)
15th Feb 2021, 4:33 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
SuperKitsune94 The -7? I don't understand. But the difference is the step If you place distance += 7 in the for loop. It will be -2+7. But if you place it like in the description. It will be 7-2. The result appears to be the same, but the point is subtracting 2 MIGHT NOT happen if the snail moving over the depth by adding to 7. In -2+7, subtracting 2 definitely happen. but it isn't in 7-2 where it might not, which is the algorithm of the correct program. Like in the sample input 31, -2 isn't applied in the last day.
15th Feb 2021, 4:20 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Thank you for helping me CarrieForle you're a good one!
15th Feb 2021, 4:42 PM
SuperKitsune94
SuperKitsune94 - avatar
+ 1
CarrieForle so basically the -2 is like an Else statement for when the break doesn't yet happen because distance is not yet >= depth??
15th Feb 2021, 5:21 PM
SuperKitsune94
SuperKitsune94 - avatar
+ 1
CarrieForle So there is no more -2 happening after the break stops the loop, correct?
15th Feb 2021, 5:23 PM
SuperKitsune94
SuperKitsune94 - avatar
0
The distance will be first substract by 2 and then add to 7. Assume the depth is 31. Day 1: 0-2+7=5 Day 2: 5-2+7=10 Day 3: 10-2+7=15 Day 4: 15-2+7=20 Day 5: 20-2+7=25 Day 6: 25-2+7=30 ** Day 7: 30-2+7 = 35 Loop ended, result is 7. Not right according to your sample input
15th Feb 2021, 4:10 PM
你知道規則,我也是
你知道規則,我也是 - avatar
0
I passed the exercise. My question is about the placement of the -7? CarrieForle
15th Feb 2021, 4:12 PM
SuperKitsune94
SuperKitsune94 - avatar
0
I meant the +=7, my bad. Thank you for explaining CarrieForle but the -2 comes after the break..so that means distance would be in a permanent -2 state right? How does this still work if distance is in a permanent state of -2? Is
15th Feb 2021, 4:26 PM
SuperKitsune94
SuperKitsune94 - avatar
0
So can you write down how you would do this formula please? I need to see your logic in an actual example CarrieForle
15th Feb 2021, 4:29 PM
SuperKitsune94
SuperKitsune94 - avatar
0
Correct ✅
15th Feb 2021, 5:29 PM
你知道規則,我也是
你知道規則,我也是 - avatar