+ 2

23 Code project in Js not passing tet 3

I have tried out for and while loop, all the tests are cleared except 3. https://code.sololearn.com/c5FLyqvR930k/?ref=app

24th Feb 2023, 6:27 PM
A Mohammed Alhaan
A Mohammed Alhaan - avatar
5 ответов
+ 3
Check the sample given that depth is 31. On day 6: after adding 7, dist is 32 . You should stop here but dist==depth false so it subtract 2 then dist 30. != depth and less than 31 so unnecessarily looping one more time causing days = 7 But correct answer is 6 only. Change condition dist==depth to *............* Can you fill this?
24th Feb 2023, 6:49 PM
Jayakrishna 🇮🇳
+ 4
You have the depth set to 0 and you only break your loop if the dist is equal to 0. What if the dist is -1 or lower?
24th Feb 2023, 6:32 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
Oh snap yes I made the dist == depth the only condition there, changed it to dist >= depth and it worked like charm Thanks you guys, I really mean it, I was stuck for almost an hour... doing the same program in different loops and stuff. Thanks again
24th Feb 2023, 7:20 PM
A Mohammed Alhaan
A Mohammed Alhaan - avatar
+ 3
It happens mate. Happy coding!
25th Feb 2023, 9:17 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
//try this function main() { var depth = parseInt(readLine(), 10); //your code goes here let distance = 0 let climb = 7; let day = 0; for(distance = 0; distance<=depth;){ distance+=climb day++ if(distance>= depth){ console.log(day) break; } distance-=2 } }
25th Feb 2023, 5:27 AM
Naimul Hasan
Naimul Hasan - avatar