Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 260 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement is not working?

#1
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

Reply

#2
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!
Reply

#3
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)`.
Reply

#4
`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`.
Reply

#5
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');
}
Reply

#6
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.

Reply

#7
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

[To see links please register here]


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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through