最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If statement is always true no matter what string I pass in - Stack Overflow

programmeradmin7浏览0评论

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"));

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"));
Share Improve this question edited Nov 27, 2015 at 22:28 m0meni 16.5k18 gold badges87 silver badges148 bronze badges asked Nov 27, 2015 at 1:10 SiunamiSiunami 4272 gold badges6 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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 pare the values, but === will pare 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?");
  }
}

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"));

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"));
发布评论

评论列表(0)

  1. 暂无评论