Question

I am getting a little confused with some code I am not sure why it will not work.

It is an If function ...

if (calorie_total<100)
{
$("#awards_info").html('bad');
}
else if (200>calorie_total>100)
{
$("#awards_info").html('okay');
}
else if (400>calorie_total>300)
{
$("#awards_info").html('good');
}
else (calorie_total>400);
{
$("#awards_info").html('great');
}

Bascailly it is looking at the total calories and then telling you how well you have done.

But for some reason it allways says "great" even when calories are less than 0?

Any ideas would be great?

Thank you

Was it helpful?

Solution

JavaScript relational operators don't work the way your code implies they should. You have to do two separate comparisons connected with &&

else if (200 > calorie_total && calorie_total > 100)

In this particular case, you don't really need to do that. If you make it to the else, then that means "calorie_total" must not be less than or equal to 100. In fact, this code won't do anything if "calorie_total" is exactly 100!

OTHER TIPS

When you say something like

if (200>calorie_total>100)

Lets say calorie_total is 150, it is basically evaluated like this

(200 > 150) > 100

which is

true > 100

which returns

false

So all the conditions fail and the else part is getting executed always. To get what you really want, you need to change the conditions like this

if (calorie_total < 200 && calorie_total > 100)

and your else part should not have any conditions (calorie_total>400);.

Read more about the coercion rules here http://webreflection.blogspot.in/2010/10/javascript-coercion-demystified.html

console.log(true == 1)
console.log(false == 0)

Output

true
true

Now that we know that, true == 1 and false == 0, we can evaluate the rest of the conditions.

You can't do something like 200>calorie_total>100 in JavaScript (at least it doesn't work how you imagine). Your previous check implies that calorie_total > 100, so you can omit it, resulting in else if (calorie_total < 200). If you really want to double check, you'll have to use logic AND operator - &&. e.g. if (200 > calorie_total && calorie_total >100).

200>calorie_total>100 and 400>calorie_total>300 will not do what you're wanting to achieve. JavaScript will read the first > and compare these values. If we break down your if statement for 200>calorie_total>100, we'd end up with:

200 > calorie_total > 100 /* Evaluates to... */
(200 > calorie_total) > 100 /* Evaluates to... */
(false) > 100 /* Or... */
(true) > 100

Both of these would evaluate to false.

What you need to do instead is make use of the && operator:

if (200 > calorie_total && calorie_total > 100) { ... }

If we were to break this down, we'd end up with:

200 > calorie_total && calorie_total > 100 /* Evaluates to ... */
true and true /* Or... */
true and false /* Or... */
false

If the values both evaluate to true, your if statement will be true. If one of these evaluates to false, your if statement will be false.

I think the last lines are strange, you might want to change

else (calorie_total>400);
{
$("#awards_info").html('great');
}

by

else if (calorie_total>400)
{
    $("#awards_info").html('great');
}

You need to make that last else statement into an else if statement.

    else if (calorie.total>400) {

            /* Code */

    }

By making it an else, it should just operate if no other conditions work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top