Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loop guard in while loop appears to be failing #4089

Closed
thetazero opened this issue Feb 9, 2023 · 3 comments
Closed

loop guard in while loop appears to be failing #4089

thetazero opened this issue Feb 9, 2023 · 3 comments

Comments

@thetazero
Copy link

Version

v14.17.5

Platform

Linux XPS 5.15.0-58-generic nodejs/node#64-Ubuntu SMP Thu Jan 5 11:43:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Enter the node REPL then type

i = 0
while (i < 5) {
    console.log(i)
    i += 1
}

How often does it reproduce? Is there a required condition?

always seems to reproduce

What is the expected behavior?

Entering this code into a file, then executing it with node file.js gives the following output as expected

0
1
2
3
4

What do you see instead?

0
1
2
3
4
5

Additional information

No response

@jasnell
Copy link
Member

jasnell commented Feb 9, 2023

This is an unfortunate artifact of the REPL... it always prints the results of the last statement when execution is complete. For instance, if we run the following in the REPL...

> (() => { let i = 0; while (i < 5) { console.log(i); i += 1 } })()

we get.

0
1
2
3
4
undefined

Note the undefined there...

In your example, the last line evaluated that produces a result is the i += 1 that increments i to 5, and therefore 5 ends up being printed. The loop is exiting correctly, it's just the result display looking off.

Another example, perhaps clearer:

> i = 0
0
> while (i < 5) { console.log('...', i); i += 1 }
... 0
... 1
... 2
... 3
... 4
5
>

@jasnell jasnell transferred this issue from nodejs/node Feb 9, 2023
@thetazero
Copy link
Author

Interesting, thank you

Copy link

github-actions bot commented Jan 6, 2024

There has been no activity on this issue for 11 months. The help repository works best when sustained engagement moves conversation forward. The issue will be closed in 1 month. If you are still experiencing this issue on the latest supported versions of Node.js, please leave a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants