Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Local Storage Project

Sergi Oca
Sergi Oca
7,981 Points

Unexpected end of input

I really can't find where the problem is but I suspect is with the brackets somehow? I get the unexpected end of input on the console.

        'use strict';
         function supportsLocalStorage() {
         try {
         return "localStorage" in window && window["localStorage"] !==null;
    } catch(e){
      return false;
    }

    function getRecentSearches() {
      var searches = localStorage.getItem("recentSearches");
      if(searches){
        return JSON.parse(searches);
      } else {
        return [];
      }
    }

    function saveSearchString(str) {
      var searches = getRecentSearches();
      if(!str || searches.indexOf(str) > -1){
       return false;
      }

      searches.push(str);
      localStorage.setItem("recentSearches", JSON.stringify(searches));
      return true;
    }

    function removeSearches() {
      localStorage.removeItem("recentSearches");
    }

    // Create an li, given string contents, append to the supplied ul
    function appendListItem(listElement, string) {
      var listItemElement = document.createElement('LI');
      listItemElement.innerHTML = string;
      listElement.appendChild(listItemElement);
    }

    // Empty the contents of an element (ul)
    function clearList(listElement) {
      listElement.innerHTML = '';
    }

    window.onload = function() {
      if(supportsLocalStorage()) {
      var searchForm = document.getElementById('searchForm');
      var searchBar = document.getElementById('searchBar');
      var recentSearchList = document.getElementById('recentSearchList');
      var clearButton = document.getElementById('clearStorage');

      // Initialize display list
      var recentSearches = getRecentSearches();
      recentSearches.forEach(function(searchString) {
        appendListItem(recentSearchList,searchString);
      });

      // Set event handlers
      searchForm.addEventListener('submit', function(event) {
        var searchString = searchBar.value;
        if (saveSearchString(searchString)) {
          appendListItem(recentSearchList, searchString);
        }
      });

      clearButton.addEventListener('click', function(event) {
        removeSearches();
        clearList(recentSearchList);
       });
      }
    };```

1 Answer

Steven Parker
Steven Parker
231,275 Points

:+1: Your intuition is good, this error is commonly due to bracket mismatch.

In this case the very first function, supportsLocalStorage, has no closing bracket.

:point_right: Note that it also has no default return value.

Sergi Oca
Sergi Oca
7,981 Points

Thank you It works now, how did you spot that? Just by looking at it? If I miss a bracket when I'm supposed to type it then I'm so lost afterwards.