Build a Bookmark Manager App - Build a Bookmark Manager App

Tell us what’s happening:

I am unable to pass test 5. I’m trying to check if the parsed array from local storage 1. is an array and 2. if each object in the array has the three required properties. If it passes then I return the array. If it fails I return an empty array.

All the other tests pass, it’s just this one validation test.

Your code so far

const getBookmarks = () => {

  const bookmarksData = JSON.parse(localStorage.getItem("bookmarks"));

  if (Array.isArray(bookmarksData) && bookmarksData.every((object) => {
    return object.hasOwnProperty("name") && object.hasOwnProperty("category") && object.hasOwnProperty("url")
  })) {
    return bookmarksData
  }
  return [];

};

<!-- file: index.html -->

/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Bookmark Manager App - Build a Bookmark Manager App

1 Like

The problem is that JSON.parse() will crash your entire function if the data in localStorage isn’t valid JSON, so you need to wrap it in a try/catch block to catch the error and then return an empty array.

1 Like

Thank you, I implemented this solution and it worked!

1 Like

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