Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

Need someone to help me with javascript arrays in array

The thing is that i am making a calculator. i wants when the user enter one or more digits the program push them in the array tab1 and as soon as he presses a non-numerical value, one of the operators, the array tab1 is pushed in the array tab before the operator. and at the end when he press the calculate() = the screen display the tab with tab.join('-'). But it fails.

<!DOCTYPE html> 
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>New Calculator</title>
  <link rel="stylesheet" href="calc.css">
</head>

<body>
  <p id="ecran"></p>
  <div id="btn">
    <input type="button" id="c1" onclick="printValue(this)" value="1" class="pad" />
    <input type="button" id="c2" onclick="printValue(this)" value="2" class="pad" />
    <input type="button" id="c3" onclick="printValue(this)" value="3" class="pad" /><br>
    <input type="button" id="c4" onclick="printValue(this)" value="4" class="pad" />
    <input type="button" id="c5" onclick="printValue(this)" value="5" class="pad" />
    <input type="button" id="c6" onclick="printValue(this)" value="6" class="pad" /><br>
    <input type="button" id="c7" onclick="printValue(this)" value="7" class="pad" />
    <input type="button" id="c8" onclick="printValue(this)" value="8" class="pad" />
    <input type="button" id="c9" onclick="printValue(this)" value="9" class="pad" /><br>
    <input type="button" id="c10" onclick="printValue(this)" value="0" class="pad" />
    <input type="button" id="c11" onclick="printValue(this)" value="+" class="pad" />
    <input type="button" id="c12" onclick="printValue(this)" value="-" class="pad" /><br>
    <input type="button" id="c13" onclick="printValue(this)" value="/" class="pad" />
    <input type="button" id="c14" onclick="printValue(this)" value="x" class="pad" />
    <input type="button" id="c15" onclick="reset()" value="reset" class="pad" /><br>
    <input type="button" id="c16" onclick="effacer()" value="clear" class="pad" />
    <input type="button" id="c17" onclick="calculate()" value="=" class="pad" />
  </div>
</body>
<script>
  let ecran = document.getElementById("ecran");
  let tab = [];

  function printValue(val) {

    let valu = val.value;
    let tab1 = [];

    if (!isNaN(valu)) {
      tab1.push(valu)

    } else {

      tab.push(tab1)
      tab.push(valu)
      tab1 = []
    }

    ecran.textContent += valu;

  }

  function calculate(tab) {
    ecran.textContent = tab.join('p');

  }


  function effacer() {
    tab.pop()
    ecran.textContent = tab.join('');

  }

  function reset() {
    tab = [];
    ecran.textContent = '';
  }
</script>
<!-- if(tab.join('').match(/^d+([^0-9]{1}d+)+$/)) {
        f(elt == "+") {
            res += parseInt(matchedTab[1][1]) 
        }
        else if(elt == "-") {
            res -= parseInt(matchedTab[1][1]) 
        }
        else if(elt == "*") {
            res *= parseInt(matchedTab[1][1]) 
        }
        else if(elt == "/") {
            res /= parseInt(matchedTab[1][1]) 
        }
         -->


</html>
question from:https://stackoverflow.com/questions/65833300/need-someone-to-help-me-with-javascript-arrays-in-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...