Javascript code rendering Chrome unresponsive

748 views
Skip to first unread message

Gaitano Ayieko

unread,
Apr 25, 2018, 1:25:53 PM4/25/18
to Chrome DevTools
I am a total newbie to coding. I am currently learning javascript in codecademy.com. As such, I happened to run this code in Chrome console:

var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';
while ( currentCard !== 'Spade') {
  console
.log(currentCard);
 
var randomNumber = Math.floor(Math.random() * 4);
  currentcard
= cards[randomNumber];
}
console
.log('Found a Spade!');
Chrome is giving me some numbers before the output "Heart" as shown in the figure attached. The number becomes bigger as the code runs. My questions.
1. What does this number mean since its not part of my code?
2. Anything wrong with the code? I suspect something is wrong with it. Once I run it, the Tab on which am running and every new tab I try to open becomes unresponsive until I have to kill Chrome from the Windows task manager. 
3. Suppose this code is an infinite loop which I suspect it is, how do I stop infinite loops in Chrome or in the console.

Running Windows 10 x86; Chrome Version 65.0.3325.181 (Official Build) (32-bit).
Chrome output.PNG

PhistucK

unread,
Apr 25, 2018, 1:33:08 PM4/25/18
to Google Chrome Developer Tools
1. The numbers show up whenever the same log text is emitted more than once (in a row, I believe). So it has been repeated 2181 times in your case.
2. This is an infinite loop, as the code never exits, due to a typo (currentcard instead of currentCard). Since JavaScript and the page are single threaded, this means you cannot interact with the tab until the code finishes running (and sitting idle for a few milliseconds).
3. Generally, you cannot stop an infinite loop, only by closing the tab. You might be able to pause the code for debugging by pressing F8 (or clicking on the pause icon in the Sources panel) while the focus is on the Developer Tools, but it does not always work in infinite loops and even if it does, you will need to induce an exception in the code for it to stop (say, F8 > enter delete cards in the console).


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chrome DevTools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-chrome-developer-tools+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-chrome-developer-tools/c00066e9-8649-4df6-b79f-621920e65cfb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Gaitano Ayieko

unread,
Apr 26, 2018, 1:14:09 AM4/26/18
to Chrome DevTools
Thank you very much. Such a silly typo causing such a big problem. 
Reply all
Reply to author
Forward
0 new messages