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

in method to displaySearchResults, IF statement NOT working, thanks

in method to displaySearchResults, IF statement NOT working, thanks

var locations = [];
// constructor function
function station (city, building, fullAddress) {
  this.city = city;
  this.building = building;
  this.fullAddress = fullAddress;
  locations.push(this);
}
// Instantiating new objects
var unionSqSea = new station ('seattle', 'Union Square', "601 Union St, Seattle, WA 98101");
var pacificPlSea = new station ('seattle', 'Pacific Place', "705 Olive Way, Seattle, WA 98101");
var SheratonTac = new station ('tacoma', 'City Center', "234 Main St, Tacoma, WA 98109");
var BellevueMall = new station ('bellevue', 'Lincoln Square', '600 100th Pl NE, Bellevue, WA 98004');
var concTechBell = new station ('bellevue', 'Concur Technologies', '601 108th Ave NE, Bellevue, WA 98004');
var southParkPor = new station ('portland', 'South Park Seafood', '914 SW Taylor St. Portland, OR 97204');
var hotelJupiPor = new station ('portland', 'Hotel Jupiter','800 East Burnside, Portland, OR 97214');
//Object literal
var tracker = {
  getForm: document.getElementById('search'),
  searchWord: null,
  searchMatches: [],

  getQueryDataNmatch: function (event) {
    event.preventDefault();
    this.searchWord = event.target.searchName.value;
    this.searchWord = this.searchWord.toLowerCase();
    console.log (this.searchWord);

    for (var i = 0; i < locations.length; i++) {
      if (locations[i].city === this.searchWord) {
        console.log (locations[i].building + ", " + locations[i].fullAddress);
        tracker.searchMatches.push(locations[i].building + ", " + locations[i].fullAddress);
        console.log(tracker.searchMatches.length);
      };
    };
    },

  displaySearchResults: function (event) {
    event.preventDefault();

          var full_list = "";
          for (var i = 0; i < tracker.searchMatches.length; i++) {
            full_list = full_list + tracker.searchMatches[i] + '<br>';
            console.log (full_list);
            var list = document.getElementById('image');
            var head1 = document.createElement('h1');
            head1.innerHTML = full_list;
            list.appendChild(head1);
            console.log (tracker.searchMatches.length);

          //   if (tracker.searchMatches.indexOf(tracker.searchWord) > -1) {
          //     var list = document.getElementById('image');
          //     var head1 = document.createElement('h1');
          //     head1.innerHTML = "That city is not in our system yet"
          //     list.appendChild(head1);
          //   };
          // };
  },


  runAllMethods: function () {
    tracker.getQueryDataNmatch (event);
    tracker.displaySearchResults (event);
  },
}
tracker.getForm.addEventListener('submit',tracker.runAllMethods);

http://codepen.io/coden/pen/jrNWgX

...

1 Answer

The parentheses of the if statement don't look like they are placed properly, which leaves a hanging statement after the conditional:

// instead of this
if (tracker.searchMatches.indexOf(tracker.queryWord))> -1 {
// use this
if (tracker.searchMatches.indexOf(tracker.queryWord) > -1) {

thanks Seth, for suggestion, however that code block is still NOT running, any idea what is causing, I can't find the bug.

From what I gather you are checking that the city name you searched for is in the list. You have a capitalization issue because you lowercase the search term but the city in the addresses are all proper-cased. However, if the city you searched for doesn't show up in the list, I would expect the list to be empty anyway. It may be better and more efficient to check that there are no search results in the list.

I am not sure what you mean by" It may be better and more efficient to check that there are no search results in the list." search is working and printing to the page, what i am having a problem, when the city is is not listed in tracker.searchMatches, to print a message "That city is not in our system yet". I reversed and "IF" 1st, but it is still not working.

displaySearchResults: function (event) {
    event.preventDefault();
    if (tracker.searchMatches.indexOf(tracker.searchWord) > -1) {
      console.log ('work');
      var list = document.getElementById('image');
      var head1 = document.createElement('h1');
      head1.innerHTML = "That city is not in our system yet"
      list.appendChild(head1);
    } 
   else {
          var full_list = "";
          for (var i = 0; i < tracker.searchMatches.length; i++) {
            full_list = full_list + tracker.searchMatches[i] + '<br>';
            console.log (full_list);
            var list = document.getElementById('image');
            var head1 = document.createElement('h1');
            head1.innerHTML = full_list;
            list.appendChild(head1);
            console.log (tracker.searchMatches.length);
         };
            };
  },