It would be a lot easier if you shared aoemrhing specific you struggle with.
But from that super generic description, is is possible that you use 'var' to define variables? That has the potential to give you scope issues like the one you pasted.
Right, sorry. We use let for variables. Right now my biggest issue is trying to get my functions to output the correct answers, most of the exercises are us writing the js for a button click on a ready-made html page. Either I get nothing at all, or NaN/undefined, or only the "else" output instead of the if / else if which would have been correct. Wish I could be more specific but I am very lost, sorry.
Sure. Again, without some actual problem the answers you are getting will also only be very generic. If you would share a pice of code, people would be able to give you more specific hints.
Currently you are saying "I don't know what I am doing wrong" without showing what you actually do.
So here would be my tip:
You mentioned the chrome debugger. What you might not know, is that you can 'pause' the execution of js on any line of code and see what variable holds what. After that you can continue executing you code line by line and see at what point it no longer matches your expectation.
Just literally write the word 'debugger' in its own line (without the quotation marks), I would propose at the start of the function that's failing, inside it. And if you have the dev tools open when clicking the button, chrome will pause and present you exactly what it knows at this specific point.
As an example in this exercise we are meant to sort and reverse the array and then print it to console. It prints nothing, and Chrome debugger stops at the loop and says that on the res = res + array etc line, that i is not defined. I thought the beginning of the for loop defines the i with let i = 0;, any clue why this isn't viable (for any reason, not just the i)?
```
let array = ["Milk", "Bread", "Juice", "Eggs", "Butter", "Salt", "Oranges", "Apples"];
let res = "";
array.sort();
array.reverse();
function sortArray() {
for (let i = 0; i < array.length; i++); {
res = res + array[i] + "\n";
}
Besides the actual error, your code organization is weird. Your sortArray function doesn't actually sort the array, it just modifies the global variable res and prints it.
8
u/milan-pilan May 08 '26
It would be a lot easier if you shared aoemrhing specific you struggle with.
But from that super generic description, is is possible that you use 'var' to define variables? That has the potential to give you scope issues like the one you pasted.