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

how do i write a code that will clear the list and overwrite new list based on search result. I am new to Javascript

I want to replace clearData function with something else that will overwrite old list and put new list. I am having difficulty making modification to getQueryDataNmatch or displaySearchResults function that will overwrite it. 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: [],
  clearButton: document.getElementById('clear'),

  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);

    };
  },

clearData: function() {
  var clearList = document.getElementById('image');
  clearList.innerHTML = "";
  tracker.searchMatches = [];
  location.reload ();
},


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

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