简体   繁体   中英

The >= and <= does not seem to work well with double digital? Javascript

What happening is that I notice that I am using if statement and check for if I have enough points to get something with >= but run into a problem that it is fine with triple digits number such as 100 or 200, but as soon as I try to do 50, it can't seem to check it which result in you not be able to buy it even if you have load of points. I did notice that it work when I increased max point to 500 or above, but I do not want to do that. Another solution that I recently found it to do it like this 050, but that is awkward way to say 50, do you guys have any idea why the check have hard time with double digits or correct way to do this?

The conflict/error is founded within Sword right before the last "Test" can also find it with only comment that is a long ass sentence.


function equipment() {

    clear();

    console.log(point);

    $('<p class=\'texts\'> Here is your equipment screen, please select what you want and if make a mistake or dislike your choice, please click on reset button, otherwise here is your ' + point + ' points to spent on here.</p>').insertBefore('#placeholder');

    $('<button class=\'command_button e-buttons\' value=\'pistol\'>Pistol</button>').insertBefore('#placeholder_choice');

    $('<button class=\'command_button e-buttons\' value=\'rifle\'>Rifle</button>').insertBefore('#placeholder_choice');

    $('<button class=\'command_button e-buttons\' value=\'sword\'>Sword</button>').insertBefore('#placeholder_choice');

    $('<button class=\'command_button e-buttons\' value=\'test\'>Test</button>').insertBefore('#placeholder_choice');

};

$(document).on('click', '.e-buttons', function() {

    var button = $(this).val();
    console.log('The Button is ' + button);
    $('#console').scrollTop($('#console')[0].scrollHeight);
    
    if (point <= 0) {

        alert("Sorry, you have no more point left, please either continue to next step or remove other choices.");

        equipment();

    }

    if (button == 'pistol') {

        if (point >= '100') {

            console.log("Your equipment menu is working. " + button);
            point = point - '100';
            console.log("the point remaining is " + point + ".");
            equipment();

        }

        else {

            alert("Sorry not enough money for pistol.");
            equipment();

        }

    }

    else if (button == 'rifle') {

        if (point >= '200') {

            console.log("Your equipment menu is working. " + button)
            point = point - '200';
            console.log("the point remaining is " + point + ".")
            equipment();

        }

        else {

            alert("Sorry not enough money for rifle.");
            equipment();

        }

    }

    else if (button == 'sword') {

        if (point >= '50') {

            console.log("Your equipment menu is working. " + button)
            point = point - '50';
            console.log("the point remaining is " + point + ".")
            equipment();

        }

        else { // Fix this, something is causing it to be error when it at 50, but if it 100 or more, it work fine. Also notice that if you reduced total to below 400, it will cause same error but are fine if over 400... So something is clearly prevent point check of sword from see the point. Maybe if change to switch statement will correct this?

            alert("Sorry not enough money for sword.");
            equipment();

        }

    }

    else if (button == 'test') {

        if (point >= '500') {

            console.log("Your equipment menu is working. " + button)
            point = point - '500';
            console.log("the point remaining is " + point + ".")
            equipment();

        }

        else {

            alert("Sorry not enough money for test.");
            equipment();

        }

    }

    else {

        console.log("Your equipment menu is not working. " + button)
        alert("Your equipment menu is not working. " + button)

    };

});```

In your code, you are using the points as a String value everywhere yet you are performing operations on it like '-'.

if (point >= '500') {

or

point = point - '500';

Is that intended? Perhaps changing the points to an integer will help.

point = point - 500;

The problem is that you're comparing strings. For example, when you compare '200' to '50' , it will actually compare character by character, from left to right. So we have the first 2 characters from 2 strings: '2' < '5' which will result in '200' >= '50' returning false .

To solve this, simply convert them to numbers before comparing them. parseInt() or Number() will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM