Build a Book Organizer - Build a Book Organizer

Tell us what’s happening:

I am doing my first lab on callback/higher-order functions and I am very confused on what test case 11 is asking for. I don’t know what that code would look like and I am trying to reference the modules for help though I am still confused.

I am aware I shouldn’t be creating a separate sum() function, but I am unsure what I should be doing in this instance or if I am going about things the wrong way.

Your code so far

const books = [
  {
    title: "Water",
    authorName: "Ada",
    releaseYear: 1942
  },
  {
    title: "Air",
    authorName: "Chris",
    releaseYear: 1974
  },
  {
    title: "Fire",
    authorName: "Leon",
    releaseYear: 1940
  }
];

function sortByYear(book1, book2) {
  if (book1.releaseYear < book2.releaseYear) {
    return -1;
  } else if (book1.releaseYear > book2.releaseYear) {
    return 1;
  } else {
    return 0;
  }
}

const filteredBooks = books.filter(book => book.releaseYear <= 1950);

console.log(filteredBooks);

filteredBooks.sort((book1, book2) => book1.releaseYear - book2.releaseYear);

console.log(filteredBooks);


Test Case:

11. You should call the sort higher order function by passing the sortByYear callback function on the filteredBooks array.

Challenge Information:

Build a Book Organizer - Build a Book Organizer

What does the sortByYear function do? You don’t seem to be using it?

What makes sort() a “higher order” function? Part of that is that it can take other functions as arguments, right?

1 Like

Of course, I am trying to wrap my head around the concept. I am referencing the module about sort()and how they use .sort((a, b) => a- b); with the rules that accompany it or rather the behaviors. I tried applying that concept here, but I still find myself unsure why this doesn’t work out.

function sortByYear(book1, book2) {

  if (book1.releaseYear < book2.releaseYear) {

    return -1;

  } else if (book1.releaseYear > book2.releaseYear) {

    return 1;

  } else {

    return 0;

  }

}


const filteredBooks = books.filter(book => book.releaseYear <= 1950);


console.log(filteredBooks);




filteredBooks.sort((a, b) => sortByYear(a, b)); 


console.log(filteredBooks);

I try to utilize the function sortByYear() and I am using a similar structure to the example in the .sort() module. The function sortByYear()returning 3 different values depending on the relationship between the two. Would this not work? It appears to show it organized in the console.

I did try putting it in-front, as an argument which should be the right way such as .sort(sortByYear(a, b));. However, theres a ReferenceError as a and b are not defined.

Just think of your sortByYear() function as a replacement for the typical compare function – (a, b) => a - b – we pass to sort().

1 Like

You’re getting closer but the syntax is to pass simply a reference of the function, the name of the function. You are writing a new arrow function and then calling sortByYear. Simply pass the name of the sortByYear function with no parentheses.

https://www.freecodecamp.org/learn/javascript-v9/lecture-working-with-higher-order-functions-and-callbacks/how-does-the-sort-method-work

1 Like

Thank you, this helped a lot! If I could select ‘solution’ on more than 1 comment I would.

Yeah, this definitely helped! I was confused on how I should be passing parameters, but it appears that wasn’t needed with the syntax.

You’re welcome and no worries. We’re just glad you figured it out! Congrats!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.