Display different messages on a Javascript site

Asked

Viewed 78 times

-1

Good afternoon. I’m learning Javascript and started a project to calculate a person’s BMI. I researched how to sum two numbers using inputs in HTML and based on the code used for the sum, converted it to make the equation that calculates a person’s BMI.

The result came out this one => https://danisanttos.github.io/exercicio-IMC/ (Works well only on desktop, I haven’t learned much about responsiveness in CSS yet).

However, I wanted to give an increment, for example, if the result is a BMI below or above the average, display after clicking on the "Result" the result of the calculation and a warning saying that BMI is above or below. It would also be nice to display "your BMI is:" after clicking on the result. But I have no idea how to do it and I tried to research but I don’t know the right terms for it.

If you can give me a hand but without crucifying me I’ll thank you very much <3 This is the html:

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title>Calculador de IMC</title>
        <meta charset="utf-8">
        <script src="js/javascript.js"></script>
        <link rel="stylesheet" href="css/style.css">
        <link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    </head>

    <body>

        <div class="content"> 
            <h1>CALCULADOR DE IMC</h1>

            <p></p><label>Digite o seu peso:</label>
            <input class="peso" type="number" min="1" max="999"><br>
                
            <p>Agora sua altura:</p>
                
            <label>M:</label>
            <input class="m" type="number" min="0" max="2">
                
            <label>CM:</label>                
            <input class="cm" type="number"  min="0" max="99">
                
            <button onclick="clicar()">RESULTADO</button>

            <p>Seu IMC é:</p>

            <p class="resultado"></p>
        
        </div> 

    </body>
</html>

Esse o Javascript:

function clicar() {
    var peso = document.querySelector(".peso").value;
    var m = document.querySelector(".m").value;
    var cm = document.querySelector(".cm").value;
    var alt = m + "." + cm;
    var res = parseFloat(peso) / (parseFloat(alt) * parseFloat(alt));
    var imc = parseFloat(res.toFixed(2))
    document.querySelector(".resultado").innerHTML = imc;
}

That’s the CSS (I don’t know if it’s necessary):

body {
    background-color: #8470FF;
    width: 100%;
    line-height: 1;
    position: relative;
    font-family: 'Ubuntu', sans-serif;
}

.content {
    background-color: #aeb79b;
    position: absolute;
    border-radius: 15px;
    padding-left: 5%;
    padding-right:5%;
    padding-bottom: 1%;
    text-align: center;
    margin: auto;
    left: 50%;
    
    transform: translatex(-50%);
}

Remembering people, I’m learning so the code is not 100%. I use this page to give a trained.

valeeeeeeu :D

2 answers

1

You can get parole switch that evaluates an expression, combining the value of the expression for a case clause, and executes the instructions associated with the case.

function clicar() {
  var peso = document.querySelector(".peso").value;
  var m = document.querySelector(".m").value;
  var cm = document.querySelector(".cm").value;
  var alt = m + "." + cm;
  var res = parseFloat(peso) / (parseFloat(alt) * parseFloat(alt));
  var imc = parseFloat(res.toFixed(2));
  var classificacao = "";

switch (true) {
    case (imc < 18.5):
        classificacao =("Abaixo do peso");
        break;
    case (imc < 24.5):
        classificacao =("Peso normal");
        break;
    case (imc < 29.9):
        classificacao =("Sobrepeso");
        break;
    case (imc < 34.9):
        classificacao =("Obesidade grau 1");
        break;
    case (imc < 39.9):
        classificacao =("Obesidade grau 2");
        break;
    case (imc >= 39.9):
        classificacao =("Obesidade grau 3");
        break;
    default:
        classificacao =("");
        break;
}

  document.querySelector(".resultado").innerHTML = imc;
  document.querySelector(".classificacao").innerHTML = classificacao;
}
body {
    background-color: #8470FF;
    width: 100%;
    line-height: 1;
    position: relative;
    font-family: 'Ubuntu', sans-serif;
}

.content {
    background-color: #aeb79b;
    position: absolute;
    border-radius: 15px;
    padding-left: 5%;
    padding-right:5%;
    padding-bottom: 1%;
    text-align: center;
    margin: auto;
    left: 50%;
    
    transform: translatex(-50%);
}
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title>Calculador de IMC</title>
        <meta charset="utf-8">
        <script src="js/javascript.js"></script>
        <link rel="stylesheet" href="css/style.css">
        <link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    </head>

    <body>

        <div class="content"> 
            <h1>CALCULADOR DE IMC</h1>

            <p></p><label>Digite o seu peso:</label>
            <input class="peso" type="number" min="1" max="999"><br>
                
            <p>Agora sua altura:</p>
                
            <label>M:</label>
            <input class="m" type="number" min="0" max="2">    
            <label>CM:</label>                
            <input class="cm" type="number"  min="0" max="99">
                
            <button onclick="clicar()">RESULTADO</button>

            <p>Seu IMC é:</p>

            <p class="resultado"></p>
            <p class="classificacao"></p>
        
        </div> 

    </body>
</html>

0

Talk to me, brother. This code is just to give you a light. I think based on it, you get what you want. Adapt to your code.

If you want, optionally, you can add a table to display the result information so users have notions of each index. Hugging

function clicar() {
  var peso = document.querySelector(".peso").value;
  var m = document.querySelector(".m").value;
  var cm = document.querySelector(".cm").value;

  //Para o caso de algum do campos virem vazios. Essa condição evita retornar "NaN" no campo;
  if (peso == "" || m == "" || cm == "") {
    alert("Todos os campos devem ser preenchidos");
    document.querySelector(".resultado").innerHTML = "";
    return;
  }

  var alt = m + "." + cm;
  var res = parseFloat(peso) / (parseFloat(alt) * parseFloat(alt));
  var imc = parseFloat(res.toFixed(2))
  var classificacao = "";

  if (imc < 16) {
    classificacao = "Magreza grave";
  } else if (imc >= 16 && imc <= 17) {
    classificacao = "Magreza Moderada";
  } else if (imc > 17 && imc <= 18.5) {
    classificacao = "Magreza Leve";
  } else if (imc > 18.5 && imc <= 25) {
    classificacao = "Saudável";
  } else if (imc > 25 && imc <= 30) {
    classificacao = "Sobrepeso";
  } else if (imc > 30 && imc <= 35) {
    classificacao = "Obesidade Grau I";
  } else if (imc > 35 && imc <= 40) {
    classificacao = "Obesidade Grau II";
  } else if (imc > 40) {
    classificacao = "Obesidade Grau III";
  }

  document.querySelector(".resultado").innerHTML = "Resultado: " + imc + " | " + classificacao;

  alert("Sua classificação é: [" + classificacao + "]");
}
<input type="text" class="peso" placeholder="Seu peso em kg"><br><br>
<input type="text" class="m" placeholder="Sua altura em metro(s)"><br><br>
<input type="text" class="cm" placeholder="Sua altura em centímetro(s)">

<button onclick="clicar()">Resultado</button>

<p class="resultado" style="color:blue; font-weight: bold;"></p>

<table>
  <thead>
    <th style="text-align:center;">IMC</th>
    <th style="text-align:center;">Classificação do IMC</th>
  </thead>

  <tbody id="tbody-imc">
    <tr>
      <td style="text-align:center;">
        <16</td>
          <td style="text-align:center;">Magreza grave</td>
    </tr>
    <tr>
      <td style="text-align:center;">16 a
        <17</td>
          <td style="text-align:center;">Magreza Moderada</td>
    </tr>
    <tr>
      <td style="text-align:center;">17 a
        <18.5</td>
          <td style="text-align:center;">Magreza Leve</td>
    </tr>
    <tr>
      <td style="text-align:center;">18.5 a
        <25</td>
          <td style="text-align:center;">Saudável</td>
    </tr>
    <tr>
      <td style="text-align:center;">25 a
        <30</td>
          <td style="text-align:center;">Sobrepeso</td>
    </tr>
    <tr>
      <td style="text-align:center;">30 a
        <35</td>
          <td style="text-align:center;">Obesidade Grau I</td>
    </tr>
    <tr>
      <td style="text-align:center;">35 a
        <40</td>
          <td class="text-center">Obesidade Grau II</td>
    </tr>
    <tr>
      <td style="text-align:center;">>40</td>
      <td style="text-align:center;">Obesidade Grau III</td>
    </tr>
  </tbody>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.