0

Why the output is showing 9 (input: 42).. ???? It should be 8...

/* 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? */ function main() { var depth = parseInt(readLine(), 10); //your code goes here var climbUp=0; var count=0; for( ;climbUp<depth ;climbUp+=7) {if(climbUp >= depth){count++;} else{climbUp -=2; count++;} } console.log(count); } /* Sample input : 42 Sample output : 8 */

4th Jan 2023, 7:44 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
2 Respostas
+ 3
It might be the way you have your loop. 1st iteration: ClimbUp (0) is less than depth, so it does the else statement > ClimbUp = -2 Count = 1.
4th Jan 2023, 8:31 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Got it.thanks.. Correction: {var count=1; var climbUp=7;}
4th Jan 2023, 9:04 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar