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
Nurbek Ismailov
2,068 Pointsin if else statement, else part is not working, also, method that calls other method is not working.
the IF part of this code was working when there was only method getQueryData. I decided to split in to 2 smaller method so it is easier to manage and create method that calls other 2 methods. So now there is bug i can't find 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: [],
getQueryData: function (event) {
event.preventDefault();
this.searchWord = event.target.searchName.value;
var queryWord = this.searchWord.toLowerCase();
console.log (queryWord);
for (var i = 0; i < locations.length; i++) {
if (locations[i].city === queryWord) {
console.log (locations[i].building + ", " +locations[i].fullAddress);
tracker.searchMatches.push(locations[i].city)
}
}
},
displaySearchResults: function (event) {
event.preventDefault();
if (tracker.searchMatches.length > 1) {
console.log ('moreThan1')
console.log (tracker.searchMatches.length);
var full_list = "";
for (var i = 0; i < tracker.searchMatches.length; i++){
full_list = full_list + locations[i].building + ", " + locations[i].fullAddress + '<br>'
var list = document.getElementById('image');
var head1 = document.createElement('h1');
head1.innerHTML = full_list;
list.appendChild(head1);
console.log (tracker.searchMatches.length);
}
}
else {
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.getQueryData ();
tracker.displaySearchResults ();
},
}
tracker.getForm.addEventListener('submit',tracker.runAllMethods);
here is my code pen http://codepen.io/coden/pen/jrNWgX
Nurbek Ismailov
2,068 Pointshere is my code, it is still has issues, in if else statement, both conditions are running now, instead of one. let me know if you can catch an error.
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: [],
displaySearchResults: function (event) {
event.preventDefault();
this.searchWord = event.target.searchName.value;
var queryWord = this.searchWord.toLowerCase();
console.log (queryWord);
for (var i = 0; i < locations.length; i++) {
if (locations[i].city === queryWord) {
console.log (locations[i].building + ", " + locations[i].fullAddress);
tracker.searchMatches.push(locations[i].city);
var full_list = "";
for (var i = 0; i < tracker.searchMatches.length; i++){
full_list = full_list + locations[i].building + ", " + locations[i].fullAddress + '<br>';
var list = document.getElementById('image');
var head1 = document.createElement('h1');
head1.innerHTML = full_list;
list.appendChild(head1);
console.log (tracker.searchMatches.length);
}
break;
}
else {
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.displaySearchResults (event);
},
}
tracker.getForm.addEventListener('submit',tracker.runAllMethods);
Peter Hatzer
20,837 PointsHi Nurbrek
I have looked at your pen noticed you updated it, your where missing a } on your for loop, you just need to update the searchmatch if there is no city, have fun keep on coding.
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);
hope this helps.
Peter
Peter Hatzer
20,837 PointsPeter Hatzer
20,837 PointsHi Nurbek,
Parts of your code is missing a few semicolons from your script, you can notice it here on treehouse as some of you code is actually green above and not orange.
I also took the time to copy your code and test it and also noticed that your methods inside runAllMethod need event passed to it like so:
Peter