Problemas con salidas de datos “NaN”en javaScript

publicado por: Anonymous

Hola estoy haciendo la típica calculadora en javaScript pero tengo un error de NaN en la salida cada vez que ejecuto la función “SUMA” y creo que estoy convirtiendo bien los datos a números y no veo el error. Por favor alguien me ayude les agradeceria.

<style>
button {
font-size: 200%;
width: 40px;
}

input {
font-size: 14px;
height: 40px;
text-align: right;
}

</style>
<script src="calc.js"></script>
<script src="jquery.js"></script>
</head>

<body>
<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>

var nunArray = []; //arreglo que almacena los resultados
var newArray = [];
var operation = false;
var suma = false;
var dividir = false;
var result = 0;
var convertir = function arrayANumber(array){
var num = "";
for(var i = 0; i < array.length; i++){
    num += array[i];
}
return (parseInt(num));
}

//Accion sumar
$('#addButton').click(function(){
    operation = true;   
    suma = true;
    if (nunArray.length) {
         result = convertir(nunArray) + convertir(newArray);
         //var total = result;

         newArray.length = 0;
         nunArray.length = 0;
         $('#display').val(result);

    }else{
        result += convertir(newArray);
        newArray.length = 0;
        $('#display').val(result);
    }/*else{
        $('#display').val(nunArray.join(""));//Muestro el valor del arreglo
    }*/
});

//Botoness
 $('#button1').click(function(){
  //probar la variable global newArray
    if (operation == true) {
         newArray.push($('#button1').val());
        var cadena = convertir(newArray);
        //$('#display').val(newArray.join(""));
        $('#display').val(cadena);
    }else{
        nunArray.push($('#button1').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button2').click(function(){
    if (operation == true) {
        newArray.push($('#button2').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button2').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button3').click(function(){    
    if (operation == true) {
        newArray.push($('#button3').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button3').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button4').click(function(){    
    if (operation == true) {
        newArray.push($('#button4').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button4').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button5').click(function(){    
    if (operation == true) {
        newArray.push($('#button5').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button5').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button6').click(function(){    
    if (operation == true) {
        newArray.push($('#button6').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button6').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button7').click(function(){    
    if (operation == true) {
        newArray.push($('#button7').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button7').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button8').click(function(){    
    if (operation == true) {
        newArray.push($('#button8').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button8').val());
        $('#display').val(nunArray.join(""));
    }
});
 $('#button9').click(function(){    
    if (operation == true) {
        newArray.push($('#button9').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button9').val());
        $('#display').val(nunArray.join(""));
    }
});
  $('#button0').click(function(){   
    if (operation == true) {
        newArray.push($('#button0').val());
        $('#display').val(newArray.join(""));
    }else{
        nunArray.push($('#button0').val());
        $('#display').val(nunArray.join(""));
    }
});
$('#clearButton').click(function(){ 
    nunArray.length = 0;
    newArray.length = 0;
    operation = false;
    suma = false;
    $('#display').val(" ");
});     

solución

El problema es que la función convertir cuando recibe un array vacío devuelve NaN en lugar de 0. Puedes solucionarlo inicializando la variable num con un "0":

_x000D_

_x000D_

var nunArray = []; //arreglo que almacena los resultados_x000D_
var newArray = [];_x000D_
var operation = false;_x000D_
var suma = false;_x000D_
var dividir = false;_x000D_
var result = 0;_x000D_
var convertir = function arrayANumber(array){_x000D_
var num = "0";_x000D_
for(var i = 0; i < array.length; i++){_x000D_
    num += array[i];_x000D_
}_x000D_
return (parseInt(num));_x000D_
}_x000D_
_x000D_
//Accion sumar_x000D_
$('#addButton').click(function(){_x000D_
    operation = true;   _x000D_
    suma = true;_x000D_
    if (nunArray.length) {_x000D_
         result = convertir(nunArray) + convertir(newArray);_x000D_
         //var total = result;_x000D_
_x000D_
         newArray.length = 0;_x000D_
         nunArray.length = 0;_x000D_
         $('#display').val(result);_x000D_
_x000D_
    }else{_x000D_
        result += convertir(newArray);_x000D_
        newArray.length = 0;_x000D_
        $('#display').val(result);_x000D_
    }/*else{_x000D_
        $('#display').val(nunArray.join(""));//Muestro el valor del arreglo_x000D_
    }*/_x000D_
});_x000D_
_x000D_
//Botoness_x000D_
 $('#button1').click(function(){_x000D_
  //probar la variable global newArray_x000D_
    if (operation == true) {_x000D_
         newArray.push($('#button1').val());_x000D_
        var cadena = convertir(newArray);_x000D_
        //$('#display').val(newArray.join(""));_x000D_
        $('#display').val(cadena);_x000D_
    }else{_x000D_
        nunArray.push($('#button1').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button2').click(function(){_x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button2').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button2').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button3').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button3').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button3').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button4').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button4').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button4').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button5').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button5').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button5').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button6').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button6').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button6').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button7').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button7').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button7').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button8').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button8').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button8').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
 $('#button9').click(function(){    _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button9').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button9').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
  $('#button0').click(function(){   _x000D_
    if (operation == true) {_x000D_
        newArray.push($('#button0').val());_x000D_
        $('#display').val(newArray.join(""));_x000D_
    }else{_x000D_
        nunArray.push($('#button0').val());_x000D_
        $('#display').val(nunArray.join(""));_x000D_
    }_x000D_
});_x000D_
$('#clearButton').click(function(){ _x000D_
    nunArray.length = 0;_x000D_
    newArray.length = 0;_x000D_
    operation = false;_x000D_
    suma = false;_x000D_
    $('#display').val(" ");_x000D_
});  

_x000D_

button {_x000D_
font-size: 200%;_x000D_
width: 40px;_x000D_
}_x000D_
_x000D_
input {_x000D_
font-size: 14px;_x000D_
height: 40px;_x000D_
text-align: right;_x000D_
}

_x000D_

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<table>_x000D_
<tbody>_x000D_
    <tr>_x000D_
        <td colspan="4"><input id="display" name="display" disabled=""></td>_x000D_
    </tr>_x000D_
_x000D_
<tr>_x000D_
  <td><button id="button1" value="1">1</button></td>_x000D_
  <td><button id="button2" value="2">2</button></td>_x000D_
  <td><button id="button3" value="3">3</button></td>_x000D_
  <td><button id="addButton">+</button></td>_x000D_
</tr>_x000D_
<tr>_x000D_
  <td><button id="button4" value="4">4</button></td>_x000D_
  <td><button id="button5" value="5">5</button></td>_x000D_
  <td><button id="button6" value="6">6</button></td>_x000D_
  <td><button id="subtractButton">-</button></td>_x000D_
</tr>_x000D_
<tr>_x000D_
  <td><button id="button7" value="7">7</button></td>_x000D_
  <td><button id="button8" value="8">8</button></td>_x000D_
  <td><button id="button9" value="9">9</button></td>_x000D_
  <td><button id="multiplyButton">*</button></td>_x000D_
</tr>_x000D_
<tr>_x000D_
  <td><button id="clearButton">C</button></td>_x000D_
  <td><button id="button0" value="0">0</button></td>_x000D_
  <td><button id="equalsButton">=</button></td>_x000D_
  <td><button id="divideButton">÷</button></td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>

_x000D_

_x000D_

_x000D_

Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *