+ 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
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?
+ 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?
+ 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
+ 3
It happens mate.
Happy coding!
+ 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
}
}