problems with if condition and functions

0

I'm having a couple of problems with the if condition, I'll leave the code:

// Variables
var serieButton = document.getElementById("serieButton");
var parallelButton = document.getElementById("parallelButton");
var realSerieZ1 = document.getElementsByTagName("input")[0];
var imaginarySerieZ1 = document.getElementsByTagName("input")[1];
var realSerieZ2 = document.getElementsByTagName("input")[2];
var imaginarySerieZ2 = document.getElementsByTagName("input")[3];
var moduleSerieZ1 = document.getElementsByTagName("input")[4];
var phaseSerieZ1 = document.getElementsByTagName("input")[5];
var moduleSerieZ2 = document.getElementsByTagName("input")[6];
var phaseSerieZ2 = document.getElementsByTagName("input")[7];
var resultRealSerie = document.getElementsByTagName("input")[9];
var resultImaginarySerie = document.getElementsByTagName("input")[10];
var resultModuleSerie = document.getElementsByTagName("input")[11];
var resultPhaseSerie = document.getElementsByTagName("input")[12];

// Conversor de angulos
Math.radiansToDegrees = function(radians) {
    return radians * 180 / Math.PI;
};

// Calculadora
var sumSerieReIm = function(){
    var realTotal = realSerieZ1.valueAsNumber + realSerieZ2.valueAsNumber;
    var imaginaryTotal = imaginarySerieZ1.valueAsNumber + imaginarySerieZ2.valueAsNumber;
    var totalModule = Math.hypot(realTotal, imaginaryTotal);
    var sin = imaginaryTotal / totalModule;
    var phaseRad = Math.asin(sin);
    var totalPhase = Math.radiansToDegrees(phaseRad);
    resultPhaseSerie.valueAsNumber = totalPhase.toFixed(2);
    resultModuleSerie.valueAsNumber = totalModule.toFixed(2);
    console.log(resultPhaseSerie.valueAsNumber, resultModuleSerie.valueAsNumber)
}
var sumSerieMoPh = function(){
    var realz1 = Math.cos(phaseSerieZ1.valueAsNumber*Math.PI/180)*moduleSerieZ1.valueAsNumber;
    var imaginaryZ1 = Math.sin(phaseSerieZ1.valueAsNumber*Math.PI/180)*moduleSerieZ1.valueAsNumber;
    var realz2 = Math.cos(phaseSerieZ2.valueAsNumber*Math.PI/180)*moduleSerieZ2.valueAsNumber;
    var imaginaryZ2 = Math.sin(phaseSerieZ2.valueAsNumber*Math.PI/180)*moduleSerieZ2.valueAsNumber;
    var realTotal = realz1 + realz2;
    var imaginaryTotal = imaginaryZ1 + imaginaryZ2;
    var totalModule = Math.hypot(realTotal,imaginaryTotal);
    var sin = imaginaryTotal / totalModule;
    var phaseRad = Math.asin(sin);
    var totalPhase = Math.radiansToDegrees(phaseRad);
    resultPhaseSerie.valueAsNumber = totalPhase.toFixed(2);
    resultModuleSerie.valueAsNumber = totalModule.toFixed(2);
}

// button
serieButton.addEventListener("click", function(e){
    e.preventDefault();
    if(isNaN(document.getElementsByTagName("input")[4,5,6,7])){
        sumSerieReIm();
    } else {
        sumSerieMoPh();
    }
    // if(isNaN(document.getElementsByTagName("input")[0,1,2,3])){
    //     sumSerieMoPh();
    // };
});
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./css/index.css">
    <title>Calculadoras</title>
</head>
<body>
    <h1>Impedancias serie y paralelo</h1>
    <div class="serieParallel">
        <form action="">
            <div>
                <h3>Suma en serie:</h3>
                <p style="display: block">Conociendo su parte real e imaginaria</p>
                <p style="display: block">Z1 = </p>
                <input type="number" step="0.001">
                <p>+J</p>
                <input type="number" step="0.001">
                <p style="display: block">Z2 = </p>
                <input type="number" step="0.001">
                <p>+J</p>
                <input type="number" step="0.001">
                <p style="display: block">Conociendo su módulo y ángulo</p>
                <p style="display: block">Z1 = </p>
                <input type="number" step="0.001">
                <p>con fase</p>
                <input type="number" step="0.001">
                <p style="display: block">Z2 = </p>
                <input type="number" step="0.001">
                <p>con fase</p>
                <input type="number" step="0.001"><br>
                <input type="submit" value="calcular" id="serieButton">
            </div>
            <div style="padding-left: 10%">
                <h3 style="margin-bottom: 36px">Resultado:</h3>
                <input type="number" step="0.001">
                <p>+J</p>
                <input type="number" step="0.001"><br>
                <input type="number" step="0.001">
                <p>con fase</p>
                <input type="number" step="0.001">
            </div>
        </form>
    </div>
    </body>
<script src="./JavaScript/index.js"></script>
</html>

The problem is that in the part of the button, if I leave the if condition in the following way:

serieButton.addEventListener("click", function(e){
    e.preventDefault();
    if(isNaN(document.getElementsByTagName("input")[4,5,6,7])){
        sumSerieReIm();
    } else {
        sumSerieMoPh();
    }
});

and I put values in the first 4 inputs that would be the values of real and imaginary, the operations are executed and the result is shown, but if I leave those 4 blank and put the values in the 4 below that would be the values of module and phase, throws me NaN. but, if I put the condition in the following way:

serieButton.addEventListener("click", function(e){
    e.preventDefault();
    if(isNaN(document.getElementsByTagName("input")[4,5,6,7])){
        sumSerieReIm();
    } 
    if(isNaN(document.getElementsByTagName("input")[0,1,2,3])){
        sumSerieMoPh();
    };
});

does the opposite, and if you execute the 4 below. My idea with conditionals is that, if the inputs of real and imaginary have a value, and those of module and phase are not, that the operation is executed for the former, otherwise, if the inputs of real and imaginary are empty and the others are the ones that have value, the operation for the latter is executed. Thank you and sorry for the extensive consultation.

    
asked by Lucho K 15.08.2018 в 05:51
source

1 answer

0

I recommend that in a Javascript console, like Chrome, do the following tests:

isNaN('')      -> false
isNaN([])      -> false
isNaN(4)       -> false
isNaN('hola')  -> true

On the other hand, if you want to check if it's a number, you have more exhaustive forms:

!isNaN(parseFloat(n)) && isFinite(n) 
    
answered by 15.08.2018 в 13:39