How do I replace the last item in an array with conditional structures and click event?

0

Hello, good evening! I need to make a calculator where when a button related to an operation is selected, it checks if the last value of the array shown in the display is already an operation, if it is, it should replace the last item with the value of the button selected operation. I can not do this, maybe for lack of attention, but if someone can give me a light, follow the code in HTML and JS:

    <body>
    <input style="right: auto; left: auto;" type="text" id="dados" value ="0" readonly>
    <table>
        <tr>
            <td><button class="button-number" value = "1">1</button></td>
            <td><button class="button-number" value = "2">2</button></td>
            <td><button class="button-number" value = "3">3</button></td>
            <td><button class="button-operations" value = "+">+</button></td>
            <td><button class="button-operations" value = "-">-</button></td>
        </tr>

        <tr>
            <td><button class="button-number" value = "4">4</button></td>
            <td><button class="button-number" value = "5">5</button></td>
            <td><button class="button-number" value = "6">6</button></td>
            <td><button class="button-operations" value = "x">x</button></td>
            <td><button class="button-operations" value = "/">/</button></td>
        </tr>
        <tr>
            <td><button class="button-number" value = "7">7</button></td>
            <td><button class="button-number" value = "8">8</button></td>
            <td><button class="button-number" value = "9">9</button></td>
            <td><button class="button-number" value = "0">0</button></td>
            <td><button id="result_button"value = "" style="width: 100%">=</button></td>
        </tr>
        <tr>
            <td colspan="5"><button id="button_reset" style="width: 100%">Limpar</button></td>
        </tr>
    </table>
</body>

JS:

( function(){

    'use strict';

//Variable--------------------------------------------------------------------------------------------------------------

    var $visor = document.querySelector("#dados");
    var $buttonsNumber = Array.prototype.slice.call(document.querySelectorAll(".button-number"));
    var $buttonsOperations = Array.prototype.slice.call(document.querySelectorAll(".button-operations"));
    var $buttonEqual = document.querySelector('#result_button');
    var $buttonReset = document.querySelector('#button_reset');

//Escope -----------------------------------------------------------------------------------------------------------    
    $buttonsNumber.forEach(function(item){
        item.addEventListener('click',clickButton,false);
    });

    $buttonsOperations.forEach(function(item){
        item.addEventListener('click', clickButton, false);
    });

    $buttonReset.addEventListener('click', function(){
        $visor.value = 0;
    },false);



  //Functions --------------------------------------------------------------------------------------------------------


function isAnOperator(coisa){
    var operations = ['+','-','x','/'];
    var lastItem = coisa[(coisa.length)-1];
    var teste = operations.some(function(item){
     return item === lastItem});

    return teste;
};

function removeLastItemIfItIsAnOperator(item, button){
    if(isAnOperator(item) == true){ 
        return item.replace(/.$/, button);} 
};

function clickButton(button){
 removeLastItemIfItIsAnOperator($visor.value, this.value);  
 $visor.value += this.value;
};
    })();
    
asked by anonymous 26.09.2018 / 01:59

0 answers