Debugger Problem or not?

Hello,

I might have a debugger problem. I am new and I downloaded vs code.
I think I need to do some readjustment for debugger, is there any videos where I can figure out some issues? i.e. when does it work properly and when not, I think because of debugger problems I get different results, when I use sort() method. I compare my results in vs code with the results on the codecademy platform,

the following code on the codecademy platform:

const checkYears = (year1, year2) => year1 < year2;
const sortYears = array => {
  const sorted = array.sort(checkYears);
  return sorted;
}

const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922] 

console.log(sortYears(years))

[ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]    <- sorted in descending order, everything good

and on my computer using vs code (same code, different output):

const checkYears = (year1, year2) => year1 < year2;

const sortYears = array => {
    const sorted = array.sort(checkYears);
    return sorted;
  }



const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]

console.log(sortYears(years))

[
  1970, 1999, 1951,    <- if I just ran the program, nothing is printed, no output 
  1982, 1963, 2011,        if I start running without  debugger than the following sorting is printed,  
  2018, 1922                   it does NOT sorted in descending  order, why?
  ]

can you help me?

Katha:)

Doesn’t matter if the outputs are different, your code is wrong.
If your code was correct it would produce the same results.

The question is: Why does the same code works on the Codecademy platform and it does not perform on vs code on my computer.
In generell I have problems with running my codes, cause they are just not printed on vs code. But when I select the button run without debugging, the code is printed on the same computer using the same vs code editor. Why?
Is there any advices hot to set up this ■■■■■■■ debugger?
It would be really nice if somebody can help, tanks!

Yeah I saw the question.
But the question is only valid for correct code. Yours isn’t.

So, if you found a bug, and you’re running it in a debugger… great, debug it? And make sure you read documentation for anything you’re not sure about, in this case sorting. If you don’t know how something promises to behave then you can’t meaningfully leverage it until you find that out.

Okay then tell me why the Codecademy platform has no problems to perform my code?
Codecademy does not tell me that my code is wrong

Go read about the sort method you’re using and compare to what you’re doing. If you’re doing X wrong, go google how to do X. You don’t need me to point out what you’re doing differently, because you can go read how it’s done and compare to what you did - that would let you find the difference.

Another thing though, is that you should have noticed that you were guessing how it works when you wrote that code. You have to have some kind of integrity check on your assumptions, and when you’re not sure, go look it up. You need to know how the things you rely on behave.

The gist of what’s happening is that you’re relying on undefined behaviour.

You are using something which isn’t promised to you, all bets are off on what result you get. This is why you get different results, because nobody promised you that it should be any particular outcome.

But if you read documentation for the things you use, and leverage what that promises to you, and this will be the same promise for both environments because it’s the same language, then you will get the same result for both.

these solutions (rules how to sort) are given by codecademy (not my solutions):

sorting in ascending order:

const compareTeeth = (speciesObj1, speciesObj2) => speciesObj1.numTeeth > speciesObj2.numTeeth


sorting in descending order (two possibilities yielding the same result)

const checkYears = (year1, year2) => year1 < year2

const sortYears = arr => arr.sort((x, y) => y - x);

Is here anybody else WHO CAN really HELP?