Siunami
Siunami

Reputation: 427

If statement is always true no matter what string I pass in

I am just learning javascript. I am trying to create a simple tip calculator that changes the tip amount depending on a user's description of how good the service was.

I pass a string in for the parameter, service, and want to check to see if it equals any of the values for each tip amount. If it does, than I try to calculate the tip based on that value.

However, after testing multiple values, it seems like my function will be true at the first if statement for any value. What am I not seeing?

/* Create an array of possible descriptions of service*/

function tip (cost, service) {
  if (service == "good" || "" || "nice"){
    return cost + cost * .10;
  }else if (service == "bad" || "horrible"){
    return cost + cost * .5;
  }else if (service == "excellent" || "great"){
    return cost + cost * .15;
  }else{
    console.log("How was the service?");
  }
}

console.log(tip(65, "great"));

Upvotes: 2

Views: 899

Answers (3)

m0meni
m0meni

Reputation: 16435

The "or" || exists for your convenience because it prevents you from writing additional if statements. Without || your first statement would be written as

if (service == "good") 
    return cost + cost * .10;
if ("")
    return cost + cost * .10;
if ("nice")
    return cost + cost * .10;

When it's written like this you can see that it's not what you mean.

if("") will always be false, and if("nice") will always be true so your other conditions will never be met.

Every time you write a conditional statement you need to write each statement between the || as if it were a separate if statement. In this case, you omitted the name of the variable so instead of

if (service == "good" || "" || "nice"){

You should have

if (service == "good" || service == "" || service == "nice"){

Another thing to note is that in Javascript it's almost always a better idea to use === than ==. This is because == will do some funky stuff called type coercion before trying to compare the values, but === will compare the two values as they are outright.

So your statement in the end should be

if (service === "good" || service === "" || service === "nice"){

Apply this same ideology to every other statement and you get:

function tip (cost, service) {
  if (service === "good" || service === "" || service === "nice"){
    return cost + cost * .10;
  }else if (service === "bad" || service === "horrible"){
    return cost + cost * .5;
  }else if (service === "excellent" || service === "great"){
    return cost + cost * .15;
  }else{
    console.log("How was the service?");
  }
}

Upvotes: 6

Harsh
Harsh

Reputation: 1319

Your or condition is wrongly written.

Since

service=="good" || "" || "nice"` 

essentially means

service=="good" || false || true, 

which will always return true.

Change it like below

function tip (cost, service) {
  if (service == "good" || service == "" || service == "nice"){
    return cost + cost * .10;
  }else if (service == "bad" || service == "horrible"){
    return cost + cost * .5;
  }else if (service == "excellent" || service == "great"){
    return cost + cost * .15;
  }else{
    console.log("How was the service?");
  }
}

console.log(tip(65, "great"));

Upvotes: 3

Leah Zorychta
Leah Zorychta

Reputation: 13409

it's because you have || '' || 'nice' and this statement will ALWAYS be true. You need to re-write your statement like this:

if (service == "good" || service == "" || service == "nice")

and you have to do this for all your statements:

function tip (cost, service) {
  if (service == "good" || "" || "nice"){
    return cost + cost * .10;
  }else if (service == "bad" || service == "horrible"){
    return cost + cost * .5;
  }else if (service == "excellent" || service == "great"){
    return cost + cost * .15;
  }else{
    console.log("How was the service?");
  }
}

console.log(tip(65, "great"));

Upvotes: 5

Related Questions