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 PointsNeed help comparing the search results against items in array, both loop and index of methods are breaking.
Here is my code, i appreciate your help.
var locations = [];
function station (city, fullAddress) {
this.city = city;
this.fullAddress = fullAddress;
locations.push(this);
}
var unionSqSea = new station ('seattle', "601 Union St, Seattle, WA 98101");
var SheratonTac = new station ('tacoma', "234 Main St, Tacoma, WA 98109");
console.log (unionSqSea);
var tracker = {
getForm: document.getElementById('search'),
searchWord: null,
getQueryData: function (event) {
event.preventDefault();
this.searchWord = event.target.searchName.value;
var queryWord = this.searchWord.toLowerCase();
console.log (queryWord);
// this is 1st method to compare, but it is not working.
for (var i = 0; i < locations.length; i++) {
if (locations[i] === queryWord) {
console.log('matched');
}
}
// this 2nd method, and it is NOT working either,
// if (locations.indexOf(queryWord)) > - 1 {
// console.log('matched');
// }
// if it matches, append the address to the page Here.
},
}
tracker.getForm.addEventListener('submit',tracker.getQueryData);
my codepen http://codepen.io/coden/pen/jrNWgX
2 Answers
Michael Liendo
15,326 PointsIn the first function, you just need to add .cityto locations[0] part in your loop condition. For the second one, locations.indexOf(queryWord) should be locations[i].city.indexOf(queryWord)
Nurbek Ismailov
2,068 PointsMichael, thanks for help. (locations[i].city === queryWord) appears to work on codepen, but not on atom. There is red dot next to for (var i = 0; i < locations.length; i++) Any idea why it is not working?
var locations = [];
function station (city, fullAddress) {
this.city = city;
this.fullAddress = fullAddress;
locations.push(this);
}
var unionSqSea = new station ('seattle', "601 Union St, Seattle, WA 98101");
var SheratonTac = new station ('tacoma', "234 Main St, Tacoma, WA 98109");
console.log (unionSqSea);
var tracker = {
getForm: document.getElementById('search'),
searchWord: null,
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) {
alert('match');
}
}
// if it matches, append the address to the page Here.
},
}
tracker.getForm.addEventListener('submit',tracker.getQueryData);
Nurbek Ismailov
2,068 Pointsit started working now, thanks Michael
Michael Liendo
15,326 PointsGlad to help. Happy Coding!
Nurbek Ismailov
2,068 PointsNurbek Ismailov
2,068 Pointshere is my code pend code http://codepen.io/coden/pen/jrNWgX