If you're looking for the solution to your problem, it's essential to master the art of debugging JavaScript code. Your web browser is equipped with a debugger that allows you to meticulously examine each step in your code execution and monitor variables along the way. I highly recommend giving this method a try as it will greatly benefit your troubleshooting process.
To get started, simply insert a debugger;
statement before your initial line of code, then launch the developer tools and refresh your page. The debugger will halt at that specific statement, enabling you to navigate through the code using the Step In button within the debugger tool.
To facilitate your practice, I have included a snippet of your original code (with the bug) below, along with the addition of the debugger;
statement:
function test() {
debugger;
array = [4, 5, 1, 3]
for (var i = 0; i < array.length; i++) {
var num = 0;
if (array[i] > num) {
num = array[i];
}
}
}
test();
I recommend encapsulating the code you wish to analyze within a function, similar to what I've done here. This approach allows you to focus on local variables without the interference of global variables or functions cluttering your view.
Once you have the developer tools open, execute the snippet and utilize the single-step feature mentioned earlier to progress through the code.
Remember, every browser offers developer tools designed to aid in the debugging process. For instance, you can refer to this guide to Chrome DevTools for comprehensive assistance.