+ 1
Snail in the Well Question?
I’m really scratching my head, trying to figure this one out, because it works in some cases, but not in others. Any tips of how to fix this? Code Is below: function main() { var depth = parseInt(readLine(), 10); //your code goes here day = 0; for (y=0; y<depth; y+=7){ if (y>=depth){ break; } else{ y-=2; day++; } } console.log(day); }
5 Réponses
+ 1
//because you are not counting last day.. Check this code.. Is what I mean.
var depth = parseInt(readLine(), 10);
day = 0;
for (y=0 ; ; ) {
y+=7;
day++;
if (y>=depth) {
console.log(day);
break;
} else{
y-=2;
}
}
+ 4
First add 7 feets, if not reached depth, subtract 2. In any case, increment day.
day++ ; irrespective of y value.
Add it in outside if blocks.. Also add y+=7 inside loop, before if block.
+ 1
Ahh i see, so as far as writing the y+=7 inside the conditions… when does this execute,exactly? (Is it the end of each iteration)
+ 1
I changed my code to this and it worked (though im not exactly sure why I needed to set the day as =1)
{
var depth = parseInt(readLine(), 10);
//my code
day = 1;
for (y=0; y<=depth; ){
y+=7;
if (y>=depth){
console.log(day);
break;
} else{
y-=2;
day++;
}
}
}
+ 1
Actually, after a second thought, it makes sense logically because otherwise calculating a day 0 would skew the results unnecessarily.