1

I have a question about a couple of lines of code not complicated. I was just trying to execute this in JS with visual studio code:

let basket=['strawberry','peach','banana'];

for (const fruit in basket) { 
    
    basket[fruit]='apple'; 
    console.log(basket);
}

The problem is when I ran it by pressing F5, the result is below with VS code:

(3) ['apple', 'peach', 'banana']

But when I manually executed it step by step by breaking point, the result is below:

(3) ['apple', 'peach', 'banana']
(3) ['apple', 'apple', 'banana']
(3) ['apple', 'apple', 'apple']

which is confusing, why did the execution stop at the 1st loop when debugging by F5(without breaking point)? does the code itself have a problem or is it my debugger? Thank you in advance.

jovi
  • 11
  • 2
  • 1
    Reproduced in my VS Code 1.58.2. Looks like there is an open bug for it https://github.com/microsoft/vscode/issues/126967 – Nikita Skrebets Jul 25 '21 at 18:37
  • If you add something `console.log(basket + "");`, it outputs as expected, though the logged object has been stringified, of course. – Nikita Skrebets Jul 25 '21 at 18:41
  • Your debugger is stopping on a break-point, or you have it set to break when you launch your debugger. Check the configuration settings that you have set in your "launch.json", make sure that you understand what everything is in that file. It should always produce the results shown in the last snippet you posted of 3 arrays with 3 fruits in each array. – JΛYDΞV Jul 25 '21 at 18:44
  • 1
    Btw, [don't use `for…in` enumerations on arrays](https://stackoverflow.com/q/500504/1048572). – Bergi Jul 25 '21 at 19:18
  • Thanks guys for your solutions! It finally works. – jovi Jul 26 '21 at 15:13

2 Answers2

1

Debug console displays only one iteration because by that time debugger is disconnected. You can verify this by putting a breakpoint after the loop - debugger will still be active and debug console will show all 3 iterations.

Instead of running debugger via F5 I suggest starting a debug terminal and running it via node file.js this way you will instantly see both actual output and debug console.

enter image description here

And here is the same view with breakpoint after the loop

enter image description here

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Thanks a lot for your solution, appreciate it. It finally works. Actually, I was struggling with how to run code in the integrated terminal, obviously it's a flaw of vs code, but I added a code runner extension then it worked. – jovi Jul 26 '21 at 15:16
0

First of all, thank you guys for your answers above, really appreciate it. Actually, I was struggling after your answers with how to run code in the integrated terminal coz it didn't work for my vs code. Now, I found a solution that is less complicated compared to the solutions on the internet. So I decided to post my fruit of research for beginners like me on how to run code in the terminal of vs-code for c++ & javascript.

  1. add an extension "code runner v0.11.5 by Jun han".
  2. close, re-open your vs code.
  3. go to "setting -> Extensions: run code configuration -> check the box "run in terminal" and check the box "save file before run".
  4. go back to your code and hit "Ctrl + Alt + N". then your code will be executed successfully in your terminal.

feel free to point it out if it doesn't work as you expected.

jovi
  • 11
  • 2