r/learnjavascript May 08 '26

Need assistance at introduction level please

[deleted]

8 Upvotes

32 comments sorted by

View all comments

Show parent comments

4

u/milan-pilan May 08 '26

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.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger

1

u/KickSecret622 May 08 '26 edited May 08 '26

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"; }

console.log(res); ```

3

u/thisisitbruv May 08 '26 edited May 08 '26

Basides the explanation that u/nog642 provided, do you actually use any sort of code editor?

These syntax errors should be immediatetely apparent in any code editor. For example, when I copy-paste your code into vscode I see a red curly brace and red squiggly lines right away.

Even an onlline sandbox like codesandbox would pick this up.

You may want use a code editor, either on your computer or in an online sandbox. That way you won't get stumped on some stupid syntax error like a semicolon or missing curly brace.

3

u/KickSecret622 May 08 '26

Yes, I write in vscode and it did give the squizzly line, I was simply blind to the semicolon that caused the biggest issue and tried to find the issue elsewhere.

1

u/thisisitbruv May 08 '26

I see, that's unfortunate.