简体   繁体   中英

When i try to use the variable changed within an if statement it returns NaN

I'm trying to make a neural network but when I try to retrieve the changed state of the variable from the if statement in my js code it just retrieves the state at which I declared the variable instead, is there a fix for this? Also, if anyone knows of a neater way to do those inputs that would be helpful as well. Edit: I could probably use the switch condition but I'm relatively new to js so I'd need some help or an example of how to implement that into my code.

JS:

var input1;
var input2;
var input3;
function myFunction1() {
    if(input1 != 1) {
        input1 = 1;
    }
    else if(input1 != 0) {
        input1 = 0;
    } 
document.getElementById("in1state").innerHTML = input1;
}
function myFunction2() {
    if(input2 != 1) {
        input2 = 1;
    }
    else if(input2 != 0) {
        input2 = 0;
    }

 document.getElementById("in2state").innerHTML = input2;
}
function myFunction3() {
    if(input3 != 1) {
        input3 = 1;
    }
    else if(input3 != 0) {
        input3 = 0;
    }

 document.getElementById("in3state").innerHTML = input3;
}
var neuron1, neuron2, neuron3, neuron4
 var n1Weight, n2Weight, n3Weight, n4Weight
 var n1Bias, n2Bias, n3Bias, n4Bias

n1Weight = 0.6;
n2Weight = 0.2;
n3Weight = 1.0;
n4Weight = 0.6;

n1Bias = 0.4;
n2Bias = 0.8;
n3Bias = 1.0;
n4Bias = 0.6;

neuron1 = (input1 * n1Weight) + (input2 * n1Weight) + (input3 * n1Weight) + n1Bias;
neuron2 = (input1 * n2Weight) + (input2 * n2Weight) + (input3 * n2Weight) + n2Bias;
neuron3 = (input1 * n3Weight) + (input2 * n3Weight) + (input3 * n3Weight) + n3Bias;
neuron4 = (input1 * n4Weight) + (input2 * n4Weight) + (input3 * n4Weight) + n4Bias;
function myRun() {

 document.getElementById("oof").innerHTML = neuron1 + " " + neuron2 + " " + neuron3 + " " + neuron4;
}

'''

NAN meaning, not a number.

That's because input1, input2, and input3 are strings. In Javascript, you can't multiply an Integer with String

1 is different than "1"

so to fix this, you would have to parse input1, input2, and input3 by using parseInt

parseInt(input1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM