0

I am very new to JavaScript and trying my best so please be patient with me. I am using if else statements to ask two questions.

  1. where you live;
  2. how many hours you are home.

Depending on the answer house or apartment, and how long less than 5 hours, he recommends a pet.

The issue that I am having is if I type house and > 5 hours it also returns the choice for apartment for the > 5 hours.

Here's my code:

var residence = prompt("Enter House, Apartment or Dorm");
var hours = prompt("Amount of hours home", "");

if ((residence == "House" || "house") && (hours <= 5)) {

    var x=window.confirm("You should get a hamster" + "\nWould you like to Purchase one?")
    if (x)
        window.alert("Thank you for your purchase!")
    else
        window.alert("Too bad")
} 
else if ((residence == "House" || "house") && (hours > 5) && (hours <= 10)) {

    var x = window.confirm("You should get a cat" + "\nWould you like to Purchase one?")
    if (x)
        window.alert("Thank you for your purchase!")
    else
        window.alert("Too bad")
}

if ((residence == "Apartment" || "apartment") && (hours <= 5)) {

    var x = window.confirm("You should get a gold fish" + "\nWould you like to Purchase one?")
    if (x)
        window.alert("Thank you for your purchase!")
    else
        window.alert("Too bad")
}

I hope this makes sense for what I am asking. Thank you for your help.

msturdy
  • 10,479
  • 11
  • 41
  • 52
joePgal
  • 15
  • 1
  • 2
  • 2
    Have you considered `if( residence.toLowerCase() == "apartment" && hours <= 5)` instead of your code that doesn't work because JAVASCRIPT ISN'T ENGLISH? – Niet the Dark Absol Feb 05 '15 at 02:29

1 Answers1

1

There is a slight logic problem :

residence == "House" || "house"

is evaluating two expressions

1: residence == "House"

2) "house"

"house" is what is called truthy, meaning when tested it will evaluate to true. So, every time when you call this code, or it's apartment counterpart, it will evaluate true.

What you should have written is residence == "House" || residence == "house". However, there is an even cleaner way to handle this :

residence.toLowerCase() == "house"

Your new code will look like this :

var residence = prompt("Enter House, Apartment or Dorm");
var hours = prompt("Amount of hours home", "");

if (residence.toLowerCase() == "house" && (hours <= 5)) {

    var x=window.confirm("You should get a hamster" + "\nWould you like to Purchase one?")
    if (x)
        window.alert("Thank you for your purchase!")
    else
        window.alert("Too bad")

} 
else if (residence.toLowerCase() == "house" && (hours > 5) && (hours <= 10)) {

    var x = window.confirm("You should get a cat" + "\nWould you like to Purchase one?")
    if (x)
        window.alert("Thank you for your purchase!")
    else
        window.alert("Too bad")

}
else if (residence.toLowerCase() == "apartment" && (hours <= 5)) {

    var x = window.confirm("You should get a gold fish" + "\nWould you like to Purchase one?")
    if (x)
        window.alert("Thank you for your purchase!")
    else
        window.alert("Too bad")

}
trex005
  • 5,015
  • 4
  • 28
  • 41