Skip to content
Advertisement

Helper function keeps giving syntaxError about missing syntax after argument?

I am trying to implement a helper function into my function that is trying to solve and sort an array of books into the top 5 most popular books. For some reason I keep receiving a syntaxError stating that I’m missing syntax after my argument. Ive gone through my code and everything looks right but maybe I am overlooking something? Any help would be appreciated thanks.

This is the prompt I’m given:

The getMostCommonGenres() function in public/src/home.js has a single parameter:

  • An array of books.

It returns an array containing five objects or fewer that represents the most common occurring genres, ordered from most common to least.

Each object in the returned array has two keys:

  • The name key which represents the name of the genre.
  • The count key which represents the number of times the genre occurs.

If more than five genres are present, only the top five should be returned.

Here is a portion of the data used:

const books = [
{
id: "5f447132d487bd81da01e25e",
title: "sit eiusmod occaecat eu magna",
genre: "Science",
authorId: 8,
borrows: [
  {
    id: "5f446f2e2cfa3e1d234679b9",
    returned: false,
  },
  {
    id: "5f446f2ed3609b719568a415",
    returned: true,
  },
  {
    id: "5f446f2e1c71888e2233621e",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2ede05a0b1e3394d8b",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
  {
    id: "5f446f2e3900dfec59489477",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2e409f8883af2955dd",
    returned: true,
  },
  {
    id: "5f446f2e3900dfec59489477",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2ef2ab5f5a9f60c4f2",
    returned: true,
  },
  {
    id: "5f446f2ea6b68cf6f85f6e28",
    returned: true,
  },
  {
    id: "5f446f2eed18105706d6ca19",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2e5aa2bb5545a0f8a6",
    returned: true,
  },
  {
    id: "5f446f2ea508b6a99c3e42c6",
    returned: true,
  },
  {
    id: "5f446f2e50cc2da9cd80efdb",
    returned: true,
  },
  {
    id: "5f446f2e0b3e2ff72fc503e7",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2ef795e593cd3cd19d",
    returned: true,
  },
  {
    id: "5f446f2e2f35653fa80bf490",
    returned: true,
  },
  {
    id: "5f446f2e7b9cd304fed3a8bc",
    returned: true,
  },
  {
    id: "5f446f2ed9aac23c0340aab2",
    returned: true,
  },
],
},
{
id: "5f4471329627160be1e8ce92",
title: "esse ea veniam non occaecat",
genre: "Classics",
authorId: 10,
borrows: [
  {
    id: "5f446f2ed3609b719568a415",
    returned: false,
  },
  {
    id: "5f446f2ec32d71dabec35b06",
    returned: true,
  },
  {
    id: "5f446f2ef2ab5f5a9f60c4f2",
    returned: true,
  },
  {
    id: "5f446f2e7a1be21e362b82f9",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2ec32d71dabec35b06",
    returned: true,
  },
  {
    id: "5f446f2e59f9380a1d03d766",
    returned: true,
  },
  {
    id: "5f446f2e141b97d842b680fd",
    returned: true,
  },
  {
    id: "5f446f2e409f8883af2955dd",
    returned: true,
  },
  {
    id: "5f446f2ee176f80b8d5d24da",
    returned: true,
  },
  {
    id: "5f446f2ef795e593cd3cd19d",
    returned: true,
  },
  {
    id: "5f446f2eef419207c5fa4ec9",
    returned: true,
  },
  {
    id: "5f446f2e50cc2da9cd80efdb",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
],
},
{
id: "5f44713265e5d8d17789beb0",
title: "tempor occaecat fugiat",
genre: "Travel",
authorId: 16,
borrows: [
  {
    id: "5f446f2e4eff1030e7316861",
    returned: true,
  },
  {
    id: "5f446f2ecc5c4787c403f844",
    returned: true,
  },
  {
    id: "5f446f2ee1661e64cde14e55",
    returned: true,
  },
  {
    id: "5f446f2e50cc2da9cd80efdb",
    returned: true,
  },
  {
    id: "5f446f2ef795e593cd3cd19d",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2e3e70bb4e1ab821c9",
    returned: true,
  },
  {
    id: "5f446f2ef2ab5f5a9f60c4f2",
    returned: true,
  },
  {
    id: "5f446f2e136866e4fe60c893",
    returned: true,
  },
],
},
{
id: "5f44713264bb872240dd62d0",
title: "proident cupidatat fugiat aliquip do",
genre: "Young Adult",
authorId: 20,
borrows: [
  {
    id: "5f446f2efa7fe184c4014dd2",
    returned: false,
  },
  {
    id: "5f446f2e59f9380a1d03d766",
    returned: true,
  },
  {
    id: "5f446f2ede05a0b1e3394d8b",
    returned: true,
  },
  {
    id: "5f446f2ed9aac23c0340aab2",
    returned: true,
  },
  {
    id: "5f446f2e136866e4fe60c893",
    returned: true,
  },
  {
    id: "5f446f2eef419207c5fa4ec9",
    returned: true,
  },
  {
    id: "5f446f2e409f8883af2955dd",
    returned: true,
  },
  {
    id: "5f446f2e59f9380a1d03d766",
    returned: true,
  },
  {
    id: "5f446f2e7d3e00a5efe35b6e",
    returned: true,
  },
  {
    id: "5f446f2ec32d71dabec35b06",
    returned: true,
  },
  {
    id: "5f446f2e3e70bb4e1ab821c9",
    returned: true,
  },
  {
    id: "5f446f2e5abedb2ba5eecd16",
    returned: true,
  },
  {
    id: "5f446f2e7eb4accbc92cb7a8",
    returned: true,
  },
  {
    id: "5f446f2ef7fc711c78b57e51",
    returned: true,
  },
  {
    id: "5f446f2e5a23764338868b10",
    returned: true,
  },
  {
    id: "5f446f2ea0502bf8cbc7676a",
    returned: true,
  },
  {
    id: "5f446f2eaf32d8ddf09e8f83",
    returned: true,
  },
  {
    id: "5f446f2eef419207c5fa4ec9",
    returned: true,
  },
  {
    id: "5f446f2e91a138c3a8ac6980",
    returned: true,
  },
  {
    id: "5f446f2e5e2952040e9f9b88",
    returned: true,
  },
  {
    id: "5f446f2e141b97d842b680fd",
    returned: true,
  },
  {
    id: "5f446f2ee734f727dfceefbd",
    returned: true,
  },
  {
    id: "5f446f2efa2e5110917e5929",
    returned: true,
  },
  {
    id: "5f446f2e5e2952040e9f9b88",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
  {
    id: "5f446f2e7b9cd304fed3a8bc",
    returned: true,
  },
],
},
{
id: "5f447132a476ece080afa067",
title: "ullamco est minim",
genre: "Nonfiction",
authorId: 25,
borrows: [
  {
    id: "5f446f2e189628dfd4e6225e",
    returned: false,
  },
  {
    id: "5f446f2ec56b2fa77d5545ef",
    returned: true,
  },
  {
    id: "5f446f2e2f35653fa80bf490",
    returned: true,
  },
  {
    id: "5f446f2ee1661e64cde14e55",
    returned: true,
  },
  {
    id: "5f446f2ee5be00208a4481e0",
    returned: true,
  },
  {
    id: "5f446f2ecc5c4787c403f844",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2e2f35653fa80bf490",
    returned: true,
  },
  {
    id: "5f446f2ed92454081fb1999b",
    returned: true,
  },
  {
    id: "5f446f2e136866e4fe60c893",
    returned: true,
  },
  {
    id: "5f446f2e7d3e00a5efe35b6e",
    returned: true,
  },
  {
    id: "5f446f2e93963fcd4e5adf00",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
  {
    id: "5f446f2ecc5c4787c403f844",
    returned: true,
  },
  {
    id: "5f446f2e136866e4fe60c893",
    returned: true,
  },
  {
    id: "5f446f2ef055e8f3064b763e",
    returned: true,
  },
  {
    id: "5f446f2e7d3e00a5efe35b6e",
    returned: true,
  },
  {
    id: "5f446f2e5e2952040e9f9b88",
    returned: true,
  },
  {
    id: "5f446f2ed9aac23c0340aab2",
    returned: true,
  },
  {
    id: "5f446f2efa2e5110917e5929",
    returned: true,
  },
  {
    id: "5f446f2e3e70bb4e1ab821c9",
    returned: true,
  },
  {
    id: "5f446f2e6dd33178245e5771",
    returned: true,
  },
  {
    id: "5f446f2ec56b2fa77d5545ef",
    returned: true,
  },
  {
    id: "5f446f2eef419207c5fa4ec9",
    returned: true,
  },
  {
    id: "5f446f2ef055e8f3064b763e",
    returned: true,
  },
  {
    id: "5f446f2e1c71888e2233621e",
    returned: true,
  },
  {
    id: "5f446f2eeb9463f976413b65",
    returned: true,
  },
  {
    id: "5f446f2e138a3841f7aeccf2",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
],
}

Here is what I’ve tried:

function helper(books) {
  let countObj = {};
  books.forEach(aBook => {
      if (countObj[aBook.genre] != null) {
        countObj[aBook.genre]++;
      } else {
        countObj[aBook.genre] = 1;
      }
    } //This is the line where the error is occurring
    return countObj;
  }
}

function getMostCommonGenres(books) {
  let countObj = helper(books);
  let countArray = [];
  for (const [key, value] of Object.entries(countObj)) {
    countArray.push({
      'name': key,
      'count': value
    });
  }
  countArray.sort((a, b) => b.count - a.count);
  return countArray.slice(0, 5);
}

I am trying to solve this prompt with a helper function, I know it doesn’t need the helper function but that’s what I am having to add, once again any help would be appreciated. Thanks!

Advertisement

Answer

It’s just a small problem with your brackets:

function helper(books) {
  let countObj = {}
  books.forEach(aBook => {
    if (countObj[aBook.genre] != null) {
      countObj[aBook.genre]++
    } else {
      countObj[aBook.genre] = 1
    }
  }) // <<- fixed
  return countObj
}
function getMostCommonGenres(books) {
  let countObj = helper(books)
  let countArray = []
  for (const [key, value] of Object.entries(countObj)) {
    countArray.push({
      name: key,
      count: value,
    })
  }
  countArray.sort((a, b) => b.count - a.count)
  return countArray.slice(0, 5)
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement