1
I’m trying to convert string to float to calculate the advantage between gasoline and ethanol, but is not returning result in Screen.
Below the code HTML and JS:
var etanol, gasolina;
function Vantagem(){
etanol = parseFloat(txtEtanol.value.replace(",","."));
gasolina = parseFloat(txtGasolina.value.replace(",","."));
if (etanol < 0.7 * gasolina) {
document.getElementById('status').src="img/medidorEtanol.jpg";
} else{
document.getElementById('status').src="img/medidorGasolina.jpg";
}
}
function Limpar(){
document.getElementById('status').src="img/medidorNeutro.jpg";
}
<!DOCTYPE html>
<html>
<head>
<meta charset = 'UTF-8'>
<title> Calculadora de Combustível </title>
</head>
<body>
<header>
<h1> Calculadora de Combustível </h1>
</header>
<img src='img/medidorNeutro.jpg' id="status">
<form name="frmFlex">
Etanol: <input type="text" name="txtEtanol" placeholder="Etanol"> <br/>
Gasolina: <input type="text" name="txtGasolina" placeholder="Gasolina"> <br/>
<button onclick="Vantagem()"> Vantagem </button> <br/>
<button onclick="Limpar()"> Limpar </button> <br/>
</form>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Use the method
etanol = +txtEtanol.value.replace(",",".");
 gasolina = +txtGasolina.value.replace(",",".");


instead of parseFloat– Maury Developer