let z = document.getElementById('id_z')
let x = document.getElementById('id_x')
let y = document.getElementById('id_y')
$("#id_a, #id_b, #id_c, #id_d, #id_e").keyup(function () {
let a = $('#id_a').val()
let b = $('#id_b').val()
let c = $('#id_c').val()
let d = $('#id_d').val()
let e = $('#id_e').val()
z.value = ((c * 3.175) / a).toFixed(0)
x.value = (b*e)+(d*(e - 1))+15
// y variable throws the error
y.value = ((c*3.175) - (a * z))/z
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Value A:</label>
<input type="number" class="numberinput form-control" required="" id="id_a">
<label>Value B:</label>
<input type="number" class="numberinput form-control" required="" id="id_b">
<label>Value C:</label>
<input type="number" class="numberinput form-control" required="" id="id_c">
<label>Value D:</label>
<input type="number" class="numberinput form-control" required="" id="id_d">
<label>Value E:</label>
<input type="number" class="numberinput form-control" required="" id="id_e">
<div>
<label>Value Z:</label>
<input type="number" class="numberinput form-control" required="" id="id_z">
<label>Value x:</label>
<input type="number" class="numberinput form-control" required="" id="id_x">
<label>Value Y:</label>
<input type="number" class="numberinput form-control" required="" id="id_y">
</div>
I am creating a calculation process using JavaScript based on what the user input and delivering the result. When I try to put a number in "gear", it shows the error in the console "specified value cannot be parsed or is out of range".
const z = document.getElementById('id_z')
const x = document.getElementById('id_x')
const y = document.getElementById('id_y')
$("#id_c, #id_d, #id_e, #id_a, #id_b").keyup(function () {
const a = $('#id_a').val()
const b = $('#id_b').val()
const c = $('#id_c').val()
const d = $('#id_d').val()
const e = $('#id_e').val()
z.value = ((c * 3.175) / a).toFixed(0)
x.value = (a*e)+(d*(e - 1)+15)
// y variable throws the error
y = ((c*3.175) - (a * z))/z
console.log(y)
console.log(typeof(y))
});
For z and x looks fine but the calculation result for X always throws NaN. I need some advice on this problem, Thank you.
2
Answers
a)
.val()
provides value in string format so you have to convert it into numbers. So useparseFloat
.b) Instead of using constant use
var
orlet
as the constant value cannot be changed each time the different input values change.c) As
x,y,z
are DOM elements so you can not use them directly in your calculation. You need to change your code like below.Working snippet:
Just do parseFloat() in javascript to prevent this.
Below is the code for your problem:
Hope this work for you.