+ 1

I'm having trouble with one of the projects in the Java course and could use some help.

Hello everyone, I need some help. I'm currently working on the project at the end of Java module 3. This is what I have: function main() { var depth = parseInt(readLine(), 10); //your code goes here var i = 1 var p = 0 do { p+=7; i++; } while (p<depth); console.log(i); } This is my incomplete solution to the given problem. I was testing it to make sure it worked as intended. It did not, and I can't figure out why. 1: Depth in the tested case is equal to 42. The output for this function is 7, but I had expected it to be 6. That means this loop is running a seventh iteration, exiting the loop when p equals 49 instead of 42. Why is this loop running an extra time? 2:Though not imperative to my completion of the project, I still need an answer to this. As an experiment, I put "p+7" in place of "p+=7" and ran it. My curiosity was rewarded with a runtime error. Can someone explain why?

28th Feb 2022, 4:37 AM
Lucas Cox
3 Respuestas
+ 4
It also slips back 2 feet each night.
28th Feb 2022, 4:53 AM
Simba
Simba - avatar
+ 4
2. p+7 is an expression, not a statement
28th Feb 2022, 4:53 AM
Oma Falk
Oma Falk - avatar
+ 2
i=0
28th Feb 2022, 4:52 AM
Oma Falk
Oma Falk - avatar