Calculator Project, scope, debugging

Tell us what’s happening:
I wrote a function with a for loop to assign an event listener to all buttons. Inside was a function opTest(arg) which worked perfectly after some effort. Now I am rewriting the code to give numbers and operators their own listeners and I can’t get it to work. I struggle with scope in general and think there may be some problem with the function being outside of the original function string. I’ve chased several error messages with declarations like let arg =(...) after it said it couldn’t read properties of arg. the current error is

"Uncaught TypeError: Cannot read properties of undefined (reading 'match')"

and console.log(TBO) returns “/” as expected.

the first code below is from here
https://codepen.io/Magopolis/pen/ZEyjJeK?editors=0011
And this is the previous version where is worked
Version 13 (lines 70-91)

I’m really curious about declaring a tag as an object with .this keyword (maybe as part of a .forEach() method?). I was able to console.log a button element earlier where it returned a list of all props from the prototype…but I forget how I did it. So if anyone can point me to a tutorial that can open up the possibilities of managing clickable elements, or tips on organizing long functions together, managing scope so I can call/reference functions and variables, or any feedback I would be grateful.

Your code so far

const TBO = document.getElementById("divide").value;

function opTest(TBO) {
  //let arg = testButtonOperation
  //let TBO = document.getElementById("divide").value;
  let regex= /[x\/+‑]/gm;
  if(TBO.match(regex) !== null || TBO == "-") {
    return true;
  } return false;
}

let punch = TBO.value;
if(opTest(punch) && str1.innerHTML != "0") {
      logArr.push(str1.innerHTML);
      logArr.push(punch);
      opStorage.text = punch;
      console.log(logArr);
      str1.innerHTML = "0";
      lastPunch = [];
      lastPunch.push(punch);
      console.log(lastPunch);
      console.log(logArr);   
} 

 else if (opTest(punch) && str1.innerHTML === "0") {
      logArr.push(punch);
}

version 13 (working)
defined at the top outside of loop:

function opTest(arg) {
  let regex= /[x\/+‑]/gm;
  if(arg.match(regex) !== null || arg == "-") {
    return true;
  } return false;
}

later inside loop

    //let prevPunch = logArr[logArr.length - 1];
    //console.log(opTest(punch));
//operators    
   
    if(opTest(punch) && str1.innerHTML != "0") {
console.log("this is line 71");
console.log(opTest(punch));
console.log("last punch was an operator");      
      logArr.push(str1.innerHTML);
      logArr.push(punch);
      opStorage.text = punch;
      console.log(logArr);
      //console.log(" L65");
      //console.log(opStorage.text + " ops"); 
        str1.innerHTML = "0";
        lastPunch = [];
        lastPunch.push(punch);
        console.log(lastPunch);
       console.log(logArr); 
    } else if (opTest(punch) && str1.innerHTML === "0") {
      //need solution for valid zero value
      logArr.pop();
      logArr.push(punch);
      console.log(logArr);
    }
  //decOk = true;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36

Challenge: Build a JavaScript Calculator

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.