0

Lesson 23 - The smail in the well, I have the code almost but how to increment with 5 instead of 1?

function main() { var depth = parseInt(readLine(), 10); //your code goes here for (i = 0; i <= 42; i+=5) { //I want to increment i with 5 if (i == 42) { break; } var days = i /5; // I want to 42 / 5 = 8 console.log(days); } }

7th Feb 2022, 9:28 AM
CodeX
CodeX - avatar
6 Respostas
7th Feb 2022, 10:23 AM
CodeX
CodeX - avatar
+ 1
👉if (i==42) 👈 will never become true because you are starting at 0 and incrementing by 5. Many coders would do the *5 at the completion rather than doing it at the loop. Do you really need to increment by +5?? You could break after 👉if (i>=42)👈 so when you get to 45 it becomes true, but your for loop is already stopping when 👉i<=42👈 Your 42/5 should be 8, but in JavaScript do you need to cast it to int so it doesn't become a float? (Sorry, I don't know JavaScript)
7th Feb 2022, 10:26 AM
HungryTradie
HungryTradie - avatar
+ 1
Its lesson 23 in Javascript, the number should increase daily by 5 and as soon as its over 42 it should stop. But it doesnt need all the numbers 5, 10, 15, ... It just needs the result of 8, because it needed 8 days until it reached 42. Thanks for the inputs I updated a bit. the lesson text is: 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? input is 42 expected output 8
7th Feb 2022, 10:36 AM
CodeX
CodeX - avatar
+ 1
G'day CodeX that isn't just +5. You may need to have +7 in your loop, then check for completion, then -2 before running the loop again! Think about: the snail climbs to 37 by afternoon 7, but slips back to 35 overnight. It then climbs to 42 (= out of the well) and it doesn't slip again because it is free. EDIT; The only question left is does the snail escape when it reaches the height, or does it need to exceed the height to escape?
7th Feb 2022, 6:56 PM
HungryTradie
HungryTradie - avatar
+ 1
Thanks I tried but cant figure it out, the window output result is blank when I run my code now. I make a break, its not so easy ^^
8th Feb 2022, 12:46 AM
CodeX
CodeX - avatar
0
where is your code ?
7th Feb 2022, 10:14 AM
Jasy Fabiano
Jasy Fabiano - avatar