0

please help

The Snail In The Well project in Javascript (conditional loops): 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? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night. Hint: You can use a loop to calculate the distance the snail covers each day, and break the loop when it reaches the desired distance. I think I understand loops well but as far as that project is concerned I can't solve it, though I have tried for so long: function main() { var depth = parseInt(readLine(), 10); //your code goes here var days; var distance; for(days=0;;days++){ distance=depth/days; if(distance<days){ console.log(days); break;}

11th Sep 2021, 6:13 AM
David Osam
David Osam - avatar
6 Antworten
+ 4
for loop consiste of 3 statement for (statement 1; statement 2; statement 3) {} | v for ([initialExpression]; [conditionExpression]; [incrementExpression]) your code : for(days=0;;days++){/* Your code */} ^ where is your conditionExpression for(days=0;[condition];days++){/* Your code */} why codition is important ?? From developer.mozilla.org: The conditionExpression expression is evaluated. If the value of conditionExpression is true, the loop statements execute. If the value of condition is false, the for loop terminates. (If the condition expression is omitted entirely, the condition is assumed to be true.)
11th Sep 2021, 6:25 AM
Pariket Thakur
Pariket Thakur - avatar
+ 2
function main() { let depth = parseInt(readLine(), 10); //your code goes here for ( let days = 0; depth > 0;days++;) { depth -= 7 if (depth > 0) { depth += 2 } } console.log(days); }
11th Sep 2021, 7:06 AM
Pariket Thakur
Pariket Thakur - avatar
0
Yes but what should statement 2 in the parentheses be since the if statement in the for loop does the same job
11th Sep 2021, 6:51 AM
David Osam
David Osam - avatar
0
Also what exactly is the question about anyway? Can someone clarify that question
11th Sep 2021, 6:54 AM
David Osam
David Osam - avatar
0
It says in the question that the distance covered by the snail each day is equal to the no. of feet each day - the number of feet each night but in the code provided by sololearn depth is unknown and there are no mentions of any variables for a day's distance or a night's distance. It's all so confusing. I have spent about 3 days on this
11th Sep 2021, 6:57 AM
David Osam
David Osam - avatar
0
O ok thanks a lot
11th Sep 2021, 9:05 AM
David Osam
David Osam - avatar