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

JavaScript if else statement ignored - Stack Overflow

programmeradmin8浏览0评论

I have a simple if statement below:

function calculateTotal() {

    if (tanksize != 1 || tanksize != 2) {
        var setupPrice = basicPrice + StatPrice() + DigiStatPrice() + IRPrice() + UVPrice() + cagePrice();
        var setupPrice2 = toFixed(setupPrice, 2);

    } else {
        var setupPrice = basicPrice;
        var setupPrice2 = toFixed(setupPrice, 2);
    }


    //display the result at the top of page
    var divobj = document.getElementById('totalPrice');
    divobj.innerHTML = "£" + setupPrice2;

    //display the result at the bottom of page
    var divobj = document.getElementById('totalPrice2');
    divobj.innerHTML = "£" + setupPrice2;
}

But when the tanksize variable is set to 1 or 2, the setupPrice variable is still calculated by adding the basicPrice + StatPrice...etc.

I have a simple if statement below:

function calculateTotal() {

    if (tanksize != 1 || tanksize != 2) {
        var setupPrice = basicPrice + StatPrice() + DigiStatPrice() + IRPrice() + UVPrice() + cagePrice();
        var setupPrice2 = toFixed(setupPrice, 2);

    } else {
        var setupPrice = basicPrice;
        var setupPrice2 = toFixed(setupPrice, 2);
    }


    //display the result at the top of page
    var divobj = document.getElementById('totalPrice');
    divobj.innerHTML = "£" + setupPrice2;

    //display the result at the bottom of page
    var divobj = document.getElementById('totalPrice2');
    divobj.innerHTML = "£" + setupPrice2;
}

But when the tanksize variable is set to 1 or 2, the setupPrice variable is still calculated by adding the basicPrice + StatPrice...etc.

Share Improve this question edited Dec 10, 2014 at 10:38 AndyKing asked Dec 10, 2014 at 10:08 AndyKingAndyKing 1393 silver badges17 bronze badges 3
  • 1 try using if(tanksize > 2) – IUW Commented Dec 10, 2014 at 10:11
  • Where are you assigning tanksize ? is that a global variable because you're not passing it as argument – Pramod Solanky Commented Dec 10, 2014 at 10:12
  • @PamioSolanky the tanksize variable is set in a webpage – AndyKing Commented Dec 10, 2014 at 10:33
Add a ment  | 

6 Answers 6

Reset to default 4

You need to use:

if (tanksize !== 1 && tanksize !== 2) {

with the && operator, or

if (!(tanksize ===1 || tanksize === 2)) {

In your code, you have the first block executing any time the value is not 1 or is not 2, which equates to it always executing.

If the value is 1, then tanksize != 2 is true so tanksize!=1 || tanksize!=2 is true.

If the value is 2, then tanksize != 1 is true so tanksize!=1 || tanksize!=2 is true.

In other words, tanksize!=1 || tanksize!=2 is always true, no matter what the value of tanksize is.

This statement is always true:

if(tanksize!=1 || tanksize!=2){

because, when tanksize = 1, tanksize is different of 2

and when tanksize = 2, tanksize is different of 1.

It is not a javascript error, you just need to change your logic to make the right test in the if...

Try if(tanksize!=1 && tanksize!=2){ instead of if(tanksize!=1 || tanksize!=2){

Your Logic is wrong..

As a matter of fact, OR Operator for two NOT EQUALS is always TRUE ( check boolean table for this) and the conditional statement "if" checks for TRUE or FALSE, hence your code will always return TRUE

Use something like

if(tanksize!=1 && tanksize!=2){

  # Your code
}

(tanksize!=1 || tanksize!=2) always will be true by this statement. Change operator || to &&

your first condition is always true, cuz for example if some x = 1, is different of 2 and vice versa.
so this condition is kind of equal to.

if(true) {
  // ...
}
发布评论

评论列表(0)

  1. 暂无评论