0
AC
÷
7
8
9
x
4
5
6
-
1
2
3
+
0
.
=
  
let calcNumBtns = document.getElementsByClassName
("calc_btn_number");
let calcOperatorBtns = document.getElementsByClassName
("calc_btn_operator");

let decimalBtn = document.getElementById("calc_decimal");
let clearBtn = document.getElementById("calc_clear");
let backspaceBtn = document.getElementById("calc_backspace");
let resultBtn = document.getElementById("calc_result");
// //////////////////////////////////////////

let displayVal = '0';
let pendingVal; // en attente
let evalStringArray = [];


// ///////////// RESULTAT ///////////////////
for (let i = 0; i < calcNumBtns.length; i++){
  calcNumBtns[i].addEventListener('click', updateDisplayVal, false);
}

function updateDisplayVal(clickObj) {
  let btnText = clickObj.target.innerText;

  if (displayVal === '0')
    displayVal = '';

  displayVal += btnText;
  resultBtn.innerText = displayVal;
}

// ///////////// OPERATIONS /////////////////
for (let i = 0; i < calcOperatorBtns.length; i++) {
  calcOperatorBtns[i].addEventListener('click', performOperation, false);
}

function performOperation(clickObj) {
  let operator = clickObj.target.innerText;

  switch (operator) {
    case '+':
      pendingVal = displayVal;
      displayVal = '0';
      resultBtn.innerText = displayVal;
      evalStringArray.push(pendingVal);
      evalStringArray.push('+');

      break;

    case '-':
      pendingVal = displayVal;
      displayVal = '0';
      resultBtn.innerText = displayVal;
      evalStringArray.push(pendingVal);
      evalStringArray.push('-');

      break;

    case 'x':
      pendingVal = displayVal;
      displayVal = '0';
      resultBtn.innerText = displayVal;
      evalStringArray.push(pendingVal);
      evalStringArray.push('*');

      break;

    case '÷':
      pendingVal = displayVal;
      displayVal = '0';
      resultBtn.innerText = displayVal;
      evalStringArray.push(pendingVal);
      evalStringArray.push('/');

      break;

    case '=':
      evalStringArray.push(displayVal);
      let evaluation = eval(evalStringArray.join(''));
                    // ['5', '+', '4'] '5+4'
      displayVal = evaluation + '';
      resultBtn.innerText = displayVal;
      evalStringArray = [];

      break;
  }
}// use mathimatical operator instead just string sign


// //////////// AC ////////////////
clearBtn.onclick = () => {
  displayVal = '0';
  evalStringArray = [];
  resultBtn.innerHTML = displayVal;
};

// /////// BACKSPACE //////////////
backspaceBtn.onclick = () => {
  let lengthOfDisplayVal = displayVal.length;

  displayVal = displayVal.slice(0, lengthOfDisplayVal - 1);

  if (displayVal === '')
    displayVal = '0';

  resultBtn.innerHTML = displayVal;
}; // slice for string, ici de 0 à l'avant dernier -1


// ///////// DOT /////////////////
decimalBtn.onclick = () => {

  if(!displayVal.includes('.'))
    displayVal += '.';

  resultBtn.innerHTML = displayVal;
};
  
523