0
What’s missing here?
function main() { var depth = parseInt(readLine(), 10); //your code goes here for (x=1; depth>=5*x+2; x+=1){ if(depth<5*x+2){ break; } console.log(x); } }
7 Respuestas
+ 7
To solve this problem, you can use a loop to iterate through each day, starting from day 1. On each iteration, you can calculate the distance the snail has traveled by adding the number of feet it climbs up each day and subtracting the number of feet it slips back each night. You can then check if the snail has reached the top of the well by comparing the distance traveled to the depth of the well. If the snail has reached the top, you can exit the loop and return the number of days it took to get out.
sample code
function main() {
var depth = parseInt(readLine(), 10);
for (var days = 1; ; days++) {
var distance = 7 * days - 2 * (days - 1);
if (distance >= depth) {
console.log(days);
break;
}
}
}
+ 6
What do you mean by to get out of a well?
+ 6
This loop will iterate through each day, starting from day 1, until the snail has reached the top of the well. On each iteration, the distance traveled by the snail is calculated as 7 * days - 2 * (days - 1), which is equal to the total number of feet climbed up minus the total number of feet slipped back. If the distance traveled is greater than or equal to the depth of the well, the loop is exited and the number of days is printed.
+ 1
Thanks buddy… Got the point 🤘
+ 1
I think x needs to be declared :
for (let x = 1;...)
0
The question is like this.
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?
0
The depth variable is the height of the well, I took x as the number of days