because of how sort works, here is useful documentation:
we pass our own function to the sort method to determine sorting order. The result of y - x is then used to determine were the element should be sorted. for example, the first two elements in our array are:
1970, 1999
so if we then fill in our function which determine sorting order:
(1970, 1999) => 1999 - 1970
which gives a a value higher then 0, which means y comes before x.
in short, the .sort() method will call your function to determine sorting order.
I suggest you first console.log(years.sort()) ( inside the function it’s console.log(arrYears.sort()) )
you’ll notice the numbers in the array are descending from left to right.
then chain the reverse() method to log out the numbers ascending from left to right (in the correct order).
true, but sorting them in the correct order in the first place is very likely more efficient. Programming is always a tricky balance (performance vs readability vs maintainability)
You have (1970, 1999) => 1999 - 1970, but how are the rest of the years impacted?
Mainly, I’m confused about the output that checkYears produces, for sort() to use. Does checkYears go one by one through all the entire array? Like 1999 - 1970, 1951 - 1999, 1982 - 1951, 1963 - 1982, etc.
From the solution:
/*
// As a function declaration AND using a named function:
function sortYears(arr) {
const checkYears = (year1, year2) => year2 - year1
return arr.sort(checkYears)
}
*/
Yeah so I’ve literally solved this and am still getting marked wrong despite the fact that we aren’t told HOW this needs to be solved beyond using .sort(). Here’s my code:
The solution didn’t make sense to me either and even though I’ll read up on it, I’m frustrated that I’m getting marked wrong for how I went about getting my answer.
It is showing wrong because of the instruction of the intermediate JavaScript Practice: "You’ll have to pass in your own function in .sort() method to get the functionality you want. "
I referenced a different article regarding the .sort descending issue, after understanding what I needed to do, my output was this:
// Write your code here:
//callback function to sort array output in descending order
function sortDescending(a, b) {
return a > b ? -1 : b > a ? 1 : 0;
}
// function to return array items
const sortYears = arr => {
return years.sort(sortDescending); // uses .sort method and calls sortDescending to arrange elements in descending order
}
// Feel free to uncomment the below code to test your function:
const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
console.log(sortYears(years))
// Should print [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]
According to my console the output is correct, but the lesson isnt marked off as checked. Any ideas why?
With all the trouble I’ve been having with the intermediate java scrip challenges I deiced to start simple and straight forward using what I understand, well…thought I understood, and then go back and be more efficient .