- 2
How to get 8 value on result?
Im sorry, for my bad English. Can i help me to solve this code function main() { var depth = parseInt(readLine(), 10); //your code goes here var naik =7 var turun = 2 var progress = (naik-turun) for (i=0;i<depth;i++); console.log (i+progress); } I need 8 as result Thanks
7 Réponses
+ 2
Muhammad Nur Ikhsan
You can try this
var days = 0;
var progress = 0;
for(var i = 0; i < depth; i++) {
progress = progress + 7; // climbs 7 feet in day
days++; //count days
if (progress >= depth) {
//break the loop whenever climbs till defined depth
break;
}
progress = progress - 2; // slips 2 feet at night
}
console.log(days);
+ 4
Assuming that readLine() works, to get the value 8, depth needs to be 3.
You're aware that by putting ';' after the for loop your creating an empty for loop? Meaning that console.log will just be executed once, after all the iteration of the for loop are over, to that effect you would have the same result if you removed the for loop and just did console.log(depth+progress);
But if what you want it's to see it print the intermediary values untill it gets to 8 you just have to remove the ';' from the for loop.
+ 2
Muhammad Nur Ikhsan
console.log(naik + turun - 1) will give 8
+ 1
If the snail climbs 7 cm up and drops 2 cm down in a day, then I guess input for <depth> needs to be 40.
days_needed = depth / ( naik - turun )
Just a thought ...
+ 1
Well naik = 0
Progress = Naik
So progress = 0
Since your while condition is while(progress<depth) this loop will only stop when the progress is bigger than the depth
But ur never increment depth, u decrement it so your loop never "ends"
+ 1
I got another way. But Thanks
0
I don't get it.
I try another code. But result say that execution is time out
This my code.
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var hari = 0;
var naik =0;
var progress = naik;
while (progress<depth){
hari + 1;
naik +7;
progress -2;
if (progress>depth){
console.log (hari);
}
}
}
What's wrong? Sorry, iam bad in math.