Problemas com saídas de dados “nan” em JavaScript

Oi, estou fazendo a calculadora típica em Javascript, mas eu tenho um erro nan Na saída toda vez que eu executo a função “SUME” e acho que estou convertendo os dados bem para números e não vejo o erro. Por favor, alguém me ajude Eu 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 name="display" disabled=""></td> </tr><tr> <td><button value="1">1</button></td> <td><button value="2">2</button></td> <td><button value="3">3</button></td> <td><button>+</button></td></tr><tr> <td><button value="4">4</button></td> <td><button value="5">5</button></td> <td><button value="6">6</button></td> <td><button>-</button></td></tr><tr> <td><button value="7">7</button></td> <td><button value="8">8</button></td> <td><button value="9">9</button></td> <td><button>*</button></td></tr><tr> <td><button>C</button></td> <td><button value="0">0</button></td> <td><button>=</button></td> <td><button>÷</button></td></tr></tbody>
var nunArray = ; //arreglo que almacena los resultadosvar 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;}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(" ");}); 

Deixe uma resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *