Problems with calling an external function within another

1

Hi, I am doing the classic example of a calculator but I have problems processing a function within the function of the equal button (=). Any suggestions?

var nunArray = [];
var newArray = [];
var operation = false;
var suma = false;
function arrayANumber(array){
var num = "";
for(var i = 0; i < array.length; i++){
    num += array[i];
}
return (parseInt(num));
}
//Accion sumar
 $('#equalsButton').click(function(){
if(suma == true){
    var result = 0;
    result = arrayANumber(numArray) + arrayANumber(newArray);
    var total = result;
    $('#display').val(result);
}else{
    $('#display').val("");
}
});

$('#addButton').click(function(){
operation = true;   
suma = true;
 $('#display').val(nunArray.join(""));

}); 
 $('#button1').click(function(){
  //probar la variable global newArray
    if (operation == true) {
        newArray.push($('#button1').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button1').val());
        $('#display').val(nunArray.join(""));
    }
});

------ HTML ------

<table>
    <tbody>
        <tr>
            <td colspan="4"><input id="display" name="display" disabled=""></td>
        </tr>

    <tr>
      <td><button id="button1" value="1">1</button></td>
      <td><button id="button2" value="2">2</button></td>
      <td><button id="button3" value="3">3</button></td>
      <td><button id="addButton">+</button></td>
    </tr>
    <tr>
      <td><button id="button4" value="4">4</button></td>
      <td><button id="button5" value="5">5</button></td>
      <td><button id="button6" value="6">6</button></td>
      <td><button id="subtractButton">-</button></td>
    </tr>
    <tr>
      <td><button id="button7" value="7">7</button></td>
      <td><button id="button8" value="8">8</button></td>
      <td><button id="button9" value="9">9</button></td>
      <td><button id="multiplyButton">*</button></td>
    </tr>
    <tr>
      <td><button id="clearButton">C</button></td>
      <td><button id="button0" value="0">0</button></td>
      <td><button id="equalsButton">=</button></td>
      <td><button id="divideButton">÷</button></td>
    </tr>
    </tbody>
</table>
    
asked by user3821102 27.12.2017 в 16:59
source

1 answer

4

You get an error because you define the variable as nunArray and you send it as numArray

for that reason you leave indefined

var nunArray = [];
var newArray = [];
var operation = false;
var suma = false;

function arrayANumber(array) {
  var num = "";
  for (var i = 0; i < array.length; i++) {
    num += array[i];
  }
  return (parseInt(num));
}
//Accion sumar
$('#equalsButton').click(function() {
  if (suma == true) {
    var result = 0;
    result = arrayANumber(nunArray) + arrayANumber(newArray);
    var total = result;
    $('#display').val(result);
  } else {
    $('#display').val("");
  }
});

$('#addButton').click(function() {
  operation = true;
  suma = true;
  $('#display').val(nunArray.join(""));

});
$('#button1').click(function() {
  //probar la variable global newArray
  if (operation == true) {
    newArray.push($('#button1').val());
    $('#display').val(newArray.join(""));
  } else {
    nunArray.push($('#button1').val());
    $('#display').val(nunArray.join(""));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td><button id="button1" value="1">1</button></td>
      <td><button id="button2" value="2">2</button></td>
      <td><button id="button3" value="3">3</button></td>
      <td><button id="addButton">+</button></td>
    </tr>
    <tr>
      <td><button id="button4" value="4">4</button></td>
      <td><button id="button5" value="5">5</button></td>
      <td><button id="button6" value="6">6</button></td>
      <td><button id="subtractButton">-</button></td>
    </tr>
    <tr>
      <td><button id="button7" value="7">7</button></td>
      <td><button id="button8" value="8">8</button></td>
      <td><button id="button9" value="9">9</button></td>
      <td><button id="multiplyButton">*</button></td>
    </tr>
    <tr>
      <td><button id="clearButton">C</button></td>
      <td><button id="button0" value="0">0</button></td>
      <td><button id="equalsButton">=</button></td>
      <td><button id="divideButton">÷</button></td>
    </tr>
  </tbody>
</table>
    
answered by 27.12.2017 / 17:19
source