dimanche 31 juillet 2016

Executing A Line Of Code When Two Different Conditions In An If Statement Are True

my name is Henry and I am relatively new to jQuery. Currently I have a problem with a calculator that I am trying to build. This is my HTML code, which is probably less relevant than...

    <html>

    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <div id="integers">
            <button type="button" value="">1</button>
            <button type="button" value="">2</button>
            <button type="button" value="">3</button>
            <button type="button" value="">4</button>
            <button type="button" value="">5</button>
            <button type="button" value="">6</button>
            <button type="button" value="">7</button>
            <button type="button" value="">8</button>
            <button type="button" value="">9</button>
            <button type="button" value="">0</button>
        </div>
        <br />
        <div id="operators">
            <button type="button" value="" id="plus">+</button>
            <button type="button" value="" id="minus">-</button>
            <button type="button" value="" id="times">*</button>
            <button type="button" value="" id="by">/</button>
            <button type="button" value="" id="equals">=</button>
        </div>
        <br />
        <div id="misc">
            <button type="button" value="">.</button>
            <button type="button" value="">C</button>
        </div>
        <br />
        <input type="text" placeholder="" value="" id="input" disabled />
        <script src="http://ift.tt/29q98ga"></script>
        <script src="script.js"></script>
    </body>
</html>

... the jQuery code:

$(document).ready(function () {
    $('#input').val('');
    $('#plus , #minus , #times , #by').attr("disabled", false);
    $('#integers > button').click(function () {
        if ($('#input').val() !== 'Error') {
            a = $(this).text();
            $('#input').val($('#input').val() + a);
        }
    });
});


$('#operators > button').click(function () {
    if ($(this).is('#plus')) {
        window.x = $('#input').val();
        console.log(x);
        $('#input').val('');
        $('#plus , #minus , #times , #by').attr("disabled", true);
        $('#plus').val('used');
    } else if ($(this).is('#minus')) {
        // Yet to be implemented
    } else if ($(this).is('#times')) {
        // Yet to be implemented
    } else if ($(this).is('#by')) {
        // Yet to be implemented
    } else if ($(this).is('#equals')) {
        if ($('#input').isNumeric) {            
            if ($('#plus').val() === 'used') {
                window.y = $('#input').val();
                console.log(y);
                $('#input').val('');
                $('#input').val(parseInt(x) + parseInt(y));
            } else {
                $('#input').val('Error');
            }
        }
    }
});

The problem lies at this point:

else if ($(this).is('#equals')) {
            if ($('#input').isNumeric) {            
                if ($('#plus').val() === 'used') {
                    window.y = $('#input').val();
                    console.log(y);
                    $('#input').val('');
                    $('#input').val(parseInt(x) + parseInt(y));
                } else {
                    $('#input').val('Error');
                }
            }
        }

I am trying to say that if #Input is Numeric and if #plus has the value "used" then execute the code. These two conditions should be in one if statement if that is possible.

Note that the calculator is not at all completed and is only (supposed to be) capable of adding numbers, yet. The methods I used (such as global variables created in functions or making the equal button work by checking for a value, etc.) are, I guess, representative for my level of skill so don't be surprised about bad workarounds.

I am very thankful for every kind of help to solve the problem so I can continue my work. Thanks to everyone in advance! :)

Sorry for typos or bad english, it is not my mother language.

Aucun commentaire:

Enregistrer un commentaire