1
I’ve tried anyway and can’t get the javascript function compute 3 execute correctly. The result comes out with only one decimal and does not obey the option selected. Detail: the code needs to be in pure javascript. If anyone can help, I’m immensely grateful. Follows the code:
function calcular3() {
var x = parseFloat(document.getElementById("x").value);
var y = parseFloat(document.getElementById("y").value);
var t = parseFloat(document.getElementById("t").value);
var z = x + y * t;
var decimais = document.getElementById("decimais");
if (decimais = 0) {
var z = z.toFixed(0);
} else {
if (decimais = 1) {
var z = z.toFixed(1);
} else {
if (decimais = 2) {
var z = z.toFixed(2);
} else {
if (decimais = 3) {
var z = z.toFixed(3);
} else {
if (decimais = 4) {
var z = z.toFixed(4);
} else {
if (decimais = 5) {
var z = z.toFixed(5);
} else {
if (decimais = 6) {
var z = z.toFixed(6);
} else {
if (decimais = 7) {
var z = z.toFixed(7);
} else {
if (decimais = 8) {
var z = z.toFixed(8);
} else {
if (decimais = 9) {
var z = z.toFixed(9);
} else {
if (decimais = 10) {
var z = z.toFixed(10);
}
}
}
}
}
}
}
}
}
}
}
document.getElementById("resultado").innerHTML = z;
}
<p>
<label>variável x</label>
<input id="x" type="number" placeholder="x">
</p>
<p>
<label>variável y</label>
<input id="y" type="number" placeholder="y">
</p>
<p>
<label>variável z</label>
<input id="t" type="number" placeholder="t">
</p>
<p>Resultado: x + y*t = <span id="resultado"></span></p>
<select id="decimais" onchange="calcular3();return false;">
<option value="0">0 decimal</option>
<option value="1">1 decimal</option>
<option value="2" selected>2 decimais</option>
<option value="3">3 decimais</option>
<option value="4">4 decimais</option>
<option value="5">5 decimais</option>
<option value="6">6 decimais</option>
<option value="7">7 decimais</option>
<option value="8">8 decimais</option>
<option value="9">9 decimais</option>
<option value="10">10 decimais</option>
</select>
Thank you very much for your contribution which, incidentally, was in record time! I understood clearly your explanation. I am starting the studies in javascript. Your answer solved my problem and it was a lecture on the subject!!
– Lucasnog