I am relative new in coding and I have following problem: I have 13 number inputs, that are all floats and come automatically after an user input (class=“autoValue”). They must be added together. Then the result must be used for Math.exp function. My problem is that one of the numbers is negativ (-5) and though the subtraction is valid operation in JQuery the final result is almost always negativ (the function is triggered after each user input). So I get NaN result...
Unfortunately, all solution that I found online don’t solve my problem, so I need some help. EDIT: I added a Part of the HTML (the full code is way too long). Thanks in advance!
//get gender
$('input').click(function(e){
if ($("#demo09a").is(':checked')){
$("#2").val(0.65);
}
else {
$("#2").val(0);
}
})
function getScore() {
var sum = 0;
$(".autoValue").each(function() {
sum += +this.value;
});
console.log("sum:" + sum)
var ex = Math.exp(sum);
console.log("ex:" + ex)
var result = ex / (1 + ex);
var finalResult = result.toFixed(2);
if (isNaN(finalResult)) finalResult = 0;
$('#result').val((finalResult));
}
$(document).ready(function() {
$('.trigger').click(function(event) {
getScore();
});
$('.triggeriii').keyup(function(event) {
getScore();
});
$('.triggeriii').blur(function(event) {
getScore();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="result" type=“text” readonly="true">Result <br />
<input id="1" class="autoValue" type=“text” value="-3.0877706">Field 1 <br \>
<input id="demo09a" class="trigger" type="radio" name="gender" value="1">female
<input id="demo09b" class="trigger" type="radio" name="gender" value="0">male<br />
<input id="2" class="autoValue" type=“text” name="2" readonly="true">Field 2
Your code is working. I used different negative numbers (-5, -4) in the input field1
and it worked.
Maybe you are doing some other computations in your code that might be causing your issue. Given your code, with an input in the field1
, we have the same output independently that male
or female
was checked.
//get gender
function triggerComputation(){
$('input').click(function(e){
if ($("#demo09a").is(':checked')){
$("#2").val(0.65);
}
else {
$("#2").val(0);
}
})
getScore()
}
function getScore() {
var sum = 0;
$(".autoValue").each(function() {
sum += +this.value;
});
console.log("sum:" + sum)
var ex = Math.exp(sum);
console.log("ex:" + ex)
var result = ex / (1 + ex);
console.log("result:"+result);
var finalResult = result.toFixed(2);
if (isNaN(finalResult)) finalResult = 0;
$('#result').val((finalResult));
}
$(document).ready(function() {
$('.trigger').click(function(event) {
getScore();
});
$('.triggeriii').keyup(function(event) {
getScore();
});
$('.triggeriii').blur(function(event) {
getScore();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="result" type=“text” readonly="true">Result <br />
<input id="1" class="autoValue" type=“text” value="-3.0877706">Field 1 <br \>
<input id="demo09a" class="trigger" type="radio" name="gender" value="1">female
<input id="demo09b" class="trigger" type="radio" name="gender" value="0">male<br />
<input id="2" class="autoValue" type=“text” name="2" readonly="true">Field 2
<button onclick="triggerComputation()">compute</button>