1
I have two buttons, one is Greater and the other is Minor to return the following:
I have two fields where I type a value in each. The first (larger) button has to return me which of the two numbers is the largest and the second button has to return me which of the two numbers is the smallest, but I’m not able to nest the if and Else conditions.
Follow the code below:
function maiorMenor() {
var numero1 = parseInt(document.getElementById("num1").value);
var numero2 = parseInt(document.getElementById("num2").value);
var botao1;
var botao2;
if (!numero1) {
alert("Digite o Número 1")
}
if (!numero2) {
alert("Digite o Número 2")
}
if (numero1 > numero2) {
alert("O número 1 é maior")
}
if (numero1 < numero2) {
alert("O número 2 é maior")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Calculadora Maior-Menor</title>
<script src="js/maior-menor.js"></script>
<link rel="stylesheet" href="css/estilo.css">
</head>
<body>
<div class="calc-maior-menor">
<h1>Calculadora Maior / Menor</h1>
<div>
<label for="">Número 1:</label>
<input id="num1" type="number" placeholder="Número 1">
</div>
<div>
<label for="">Número 2:</label>
<input id="num2" type="number" placeholder="Número 2">
</div>
<div>
<button id="botao1" onclick="maiorMenor()">Maior</button>
<button id="botao2" onclick="maiorMenor()">Menor</button>
</div>
</div>
</body>
</html>
If each button makes a calculation it would not be easier to separate into two functions?
– Caique Romero
What do you think Caique? and how do I do it?
– E.M C.