function addListenerToButtons(){
    // Variables
    let buttons = document.querySelectorAll("button");
    let arr = Array.from(buttons);
    let display = document.querySelector("#display-text");

    // Loop through the buttons and add a listener to each one
    for(let button of arr){
        let val = parseInt(button.value);

        // If the button is not a number add their specific listeners
        if(isNaN(val)){
            if(button.value == 'C'){
                button.addEventListener('click', clearDisplay());
            }
            
            // Backspace button
            else if(button.value == String.fromCharCode(8592)){
                button.addEventListener('click', (backspace())); 
            }

            // Equal button
            else if(button.value == '='){
                button.addEventListener('click', () => {
                    if(currentNum == ''){
                        return;
                    }
                    let answer = operate(operator, parseInt(currentNum), parseInt(display.value));
                    display.value = answer;
                    currentNum = '';
                    firstNum = true;
                });
            }

            // Operator buttons
            else{
                button.addEventListener('click', () => {
                    operator = button.value;
                    // If the last pressed was an operator
                    if(listOfOperators.includes(lastPressed)){
                        operator = button.value;
                        firstNum = true;
                    }

                    // If the first number has been entered
                    if(firstNum == false && currentNum != ''){
                        if(odd == false){
                            lastNum = display.value;
                            let answer = operate(lastOperator, parseInt(currentNum), parseInt(display.value)).toString();
                            currentNum = display.value;
                            display.value = answer;
                            currentNum = answer;
                            firstNum = true;
                            lastPressed = button.value;
                            odd = true;
                            lastOperator = operator;
                        }
                        else{
                            lastNum = display.value;
                            let answer = operate(lastOperator, parseInt(currentNum), parseInt(lastNum)).toString();
                            display.value = answer;
                            lastNum = display.value;
                            currentNum = answer;
                            firstNum = true;
                            lastPressed = button.value;
                            odd = false;
                            lastOperator = operator;
                        }
                        
                    }

                    // If the first number has not been entered
                    else{
                        currentNum = display.value;
                        firstNum = true;
                        lastOperator = button.value;
                        lastPressed = button.value;
                    }                    
                });
            }

            continue;
        }

        // If the button is a number
        else{
            button.addEventListener('click', () => {
                // Detect if this is the first number being entered and change firstNum to false
                if(firstNum == true){
                    display.value = button.textContent;
                    firstNum = false;
                    lastPressed = button.value;
                }

                // If this is not the first button being entered append the number to the display
                else{
                    display.value = display.value + button.textContent;
                    lastPressed = button.value;
                    firstNum = false;
                }
            });
        }       
    };
}
// This function adds a listener to the buttons
This function adds a listener to the buttons
This function adds a listener to the buttons
function addListenerToButtons(){
    let buttons = document.querySelectorAll("button");
    let arr = Array.from(buttons);
    let display = document.querySelector("#display-text");
    for(let button of arr){
        let val = parseInt(button.value);
        if(isNaN(val)){
            if(button.value == 'C'){
                button.addEventListener('click', clearDisplay());
            }
            else if(button.value == String.fromCharCode(8592)){
                button.addEventListener('click', (backspace())); 
            }
            else if(button.value == '='){
                button.addEventListener('click', () => {
                    if(currentNum == ''){
                        return;
                    }
                    let answer = operate(operator, parseInt(currentNum), parseInt(display.value));
                    display.value = answer;
                    currentNum = '';
                    firstNum = true;
                });
            }
            else{
                button.addEventListener('click', () => {
                    operator = button.value;
                    if(listOfOperators.includes(lastPressed)){
                        operator = button.value;
                        firstNum = true;
                    }
                    if(firstNum == false && currentNum != ''){
                        if(odd == false){
                            lastNum = display.value;
                            let answer = operate(lastOperator, parseInt(currentNum), parseInt(display.value)).toString();
                            currentNum = display.value;
                            display.value = answer;
                            currentNum = answer;
                            firstNum = true;
                            lastPressed = button.value;
                            odd = true;
                            lastOperator = operator;
                        }
                        else{
                            lastNum = display.value;
                            let answer = operate(lastOperator, parseInt(currentNum), parseInt(lastNum)).toString();
                            display.value = answer;
                            lastNum = display.value;
                            currentNum = answer;
                            firstNum = true;
                            lastPressed = button.value;
                            odd = false;
                            lastOperator = operator;
                        }
                    }
                    else{
                        currentNum = display.value;
                        firstNum = true;
                        lastOperator = button.value;
                        lastPressed = button.value;
                    }                    
                });
            }
            continue;
        }
        else{
            button.addEventListener('click', () => {
                if(firstNum == true){
                    display.value = button.textContent;
                    firstNum = false;
                    lastPressed = button.value;
                }
                else{
                    display.value = display.value + button.textContent;
                    lastPressed = button.value;
                    firstNum = false;
                }
            });
        }       
    };
}
function addListenerToButtons ( ) { let buttons = document . querySelectorAll ( "button" ) ; let arr = Array . from ( buttons ) ; let display = document . querySelector ( "#display-text" ) ; for ( let button of arr ) { let val = parseInt ( button . value ) ; if ( isNaN ( val ) ) { if ( button . value == 'C' ) { button . addEventListener ( 'click' , clearDisplay ( ) ) ; } else if ( button . value == String . fromCharCode ( 8592 ) ) { button . addEventListener ( 'click' , ( backspace ( ) ) ) ; } else if ( button . value == '=' ) { button . addEventListener ( 'click' , ( ) => { if ( currentNum == '' ) { return ; } let answer = operate ( operator , parseInt ( currentNum ) , parseInt ( display . value ) ) ; display . value = answer ; currentNum = '' ; firstNum = true ; } ) ; } else { button . addEventListener ( 'click' , ( ) => { operator = button . value ; if ( listOfOperators . includes ( lastPressed ) ) { operator = button . value ; firstNum = true ; } if ( firstNum == false && currentNum != '' ) { if ( odd == false ) { lastNum = display . value ; let answer = operate ( lastOperator , parseInt ( currentNum ) , parseInt ( display . value ) ) . toString ( ) ; currentNum = display . value ; display . value = answer ; currentNum = answer ; firstNum = true ; lastPressed = button . value ; odd = true ; lastOperator = operator ; } else { lastNum = display . value ; let answer = operate ( lastOperator , parseInt ( currentNum ) , parseInt ( lastNum ) ) . toString ( ) ; display . value = answer ; lastNum = display . value ; currentNum = answer ; firstNum = true ; lastPressed = button . value ; odd = false ; lastOperator = operator ; } } else { currentNum = display . value ; firstNum = true ; lastOperator = button . value ; lastPressed = button . value ; } } ) ; } continue ; } else { button . addEventListener ( 'click' , ( ) => { if ( firstNum == true ) { display . value = button . textContent ; firstNum = false ; lastPressed = button . value ; } else { display . value = display . value + button . textContent ; lastPressed = button . value ; firstNum = false ; } } ) ; } } ; }
This function adds a listener to the buttons
This function adds a listener to the buttons
// Variables // Loop through the buttons and add a listener to each one // If the button is not a number add their specific listeners // Backspace button // Equal button // Operator buttons // If the last pressed was an operator // If the first number has been entered // If the first number has not been entered // If the button is a number // Detect if this is the first number being entered and change firstNum to false // If this is not the first button being entered append the number to the display