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


Ashton Holgate
Ashton Holgate
6,021 Points

Help with OR in IF statements

Hi all,

I have produced some code for a search box in a hotel website project I've created.

form.addEventListener('submit', (e) => {
  let text = input.value.toLowerCase();
  for (let i = 0; i < hotel.length; i++)
    if (text === 'london' || 'paris' || 'rome' || 'barcelona') {
      if (text === hotelCity[i].textContent) {
        hotel[i].style.display = '';
      } else if (text === '') {
        hotel[i].style.display = ''
      } else {
        hotel[i].style.display = 'none'
    } else {
      alert('Unfortunately, we only have hotels in London, Paris, Rome, and Barcelona. Please search for one of these cities');
  input.value = '';

The code will check whether or not the text in the search box is one of the cities that a hotel exists in.

If it is, it will disable the display property of hotels that are not in that city leaving only those that are.

If the box is empty, all hotels will be shown.

If the box contains text that is not one of the valid cities, an alert will appear.


The issue I'm having is that the code appears to run always as if the text is one of the four cities. As such everything is removed from display.

I can not understand why this would happen and appreciate any help understanding what I have done wrong.


1 Answer

28,558 Points

When you combine multiple conditions (with AND or OR) each of the conditions have to be a valid condition on its own. In other words you are not just adding a criteria to the first condition specified.

That means that in your if statement you have to check each of the strings against the text variable, not just the first one. Like this:

text === 'london' || text === 'paris' || text === 'rome' || text === 'barcelona'

If you don't then JavaScript will treat the string itself as the condition, and due to the fact that non-empty strings are considered "Truthy" in JavaScript it will evaluate that as True. Which is why your if statement always runs.

Ashton Holgate
Ashton Holgate
6,021 Points

I see! Thank you very much for explaining both why it didn't work and the solution!