+ 2

Issue in JavaScript course project

In 23 Code Project (The Snail in the Well) in the JavaScript course, the algorithm seems to work on every test case except for Test Case #3. No matter what code I write, it always works on everything except for this test case. Since the test case is hidden, I can't see what's wrong. Does anyone know?

11th Oct 2022, 4:05 AM
Koei Moon
6 Respostas
+ 1
Koei Moon 2nd case will also work if you do while (feet <= depth) And if (feet >= depth)
11th Oct 2022, 5:29 PM
A͢J
A͢J - avatar
+ 3
Please provide your attemp so we can see what you are doing wrong.
11th Oct 2022, 5:57 AM
Chris Coder
Chris Coder - avatar
+ 2
Attempts?
11th Oct 2022, 5:14 AM
A͢J
A͢J - avatar
+ 2
As I was sending the attempts, I noticed I never tried a for loop even though I thought I did. So I wrote an algorithm using a for loop, and surprisingly it worked on all test cases. I still wonder what I did wrong in the while loops though, so I will send my attempts here (including the one that worked). Attempt 1: function main() { var depth = parseInt(readLine(),10); //your code goes here var days = 0 var feet = 0 while (feet < depth) { if (feet + 7 == depth) { feet += 7 days++ } else { feet += 7-2 days++ } } console.log(days) } Attempt 2: function main() { var depth = parseInt(readLine(),10); //your code goes here var feet = 0 var day = 0 while (feet < depth) { feet += 7 day++ if (feet == depth) { break } else { feet -= 2 } } console.log(day) } Attempt 3 (The one that worked): function main() { var depth = parseInt(readLine(), 10); //your code goes here var feet = 0 for (var day = 0; feet < depth;) { feet += 7 if (feet >= depth) { day++ console.log(day) break } else { feet -= 2 day++ } } }
11th Oct 2022, 9:30 AM
Koei Moon
0
A͢J I tried it and it worked, so thanks! I also fixed the code in attempt 1, though I had to make more adjustments for it to work. It seems like the problem was that in attempt 3 'feet' was already equal to 'depth' in the beginning, and the condition for my while loop did not include that. But in the for loop, the condition doesn't include that either, and it still works. So maybe the problem was something else?
12th Oct 2022, 8:45 AM
Koei Moon
0
function main() { var depth = parseInt(readLine(), 10); //your code goes here let days = 0; let result = 0; while (depth) { days++; result += 7; if (days != 6) { result -= 2; } if (depth <= result) break; } console.log(days); } also case 3 doesn't work
31st Oct 2022, 5:43 PM
Aladdin Sami
Aladdin Sami - avatar