What I learned/experienced with a loop in JavaScript
Something I experienced today was when I put a loop that where the condition was always true, I saw in action where my browser slowed down as well as live server. When I deleted that loop that made the condition to be always true, and also changed from the loop count +=2, to -= , I closed live server and reopened it from VS Code.
I wrote it like this in VS Code:
let count = 19;
while (count > 0) {
console.log(count);
count += 2;
}
When I changed it from count += 2; to count -= 2; , live server and my browser seem to work again and not slow down.
At first, I didn't understand what was meant when in the course you said Hayk that certain things can cause the program to crash. When I moved the loop that made the condition always true, my browser and live server started working again.
Now I think I learned and kind of understand what Hayk meant when He said that certain things can crash the program.
I feel like that was what was happening was my browser and live server was crashing because when I wrote that loop in my code editor and opened live server it at first showed what was in the console, but allowed me to still scrolled up and down , but it was very slow and like it was gone stop scrolling.
Then went back into vs code, live server stopped working when I came back. So I removed that loop that makes the condition to be always true, and closed live server and reopened it and it worked.
What I learned was a way to spot something wrong or even that you might be using the wrong operator like in while loops after the console in the curly brackets, if you have += and it is supposed to be -=, and vice versa, I just learned that you can know that is the wrong operator is if when you open live server and you right click and the box to click on inspect wont come up or show, and another way to know is if nothing prints in the console(Which this one I learned from Hayk in the course), an another way is your live server page becomes unresponsive and other pages work(The live server page becoming unresponsive, could be an indicator that something is wrong , but I know that isn't always the situation).
Those are ways I just found you can tell if you have an infinity loop or a code that crashed your browser and programs or tell if something is wrong. The one where you know something isn't quite right when it doesn't print in the console after doing console.log I remember this from Hayk.
I noticed today that as I entered the code that makes the loop keep going like Hayk said in the course, it will crash your program or browser. Another way to tell if something is wrong is if you put a code in and open live server and it loads slowly, I'm not saying it is always the case, but if live server is loading slowly after a code, it could be the code causing it to crash.
I'm not sure, but I think the while loop I wrote above and the experience that I had with live server was my program crashing , which was the browser and live server. If I am wrong or my observation is off Hayk, please let me know.
I just wanted to share what I learned this week.
1
4 comments
SeaAmber Silver
6
What I learned/experienced with a loop in JavaScript
Dev Mastery
skool.com/devmastery
Break out the mid-tier developer trap. Get to Senior, fast.
Leaderboard (30-day)
Powered by