Problem when displaying the highest and lowest note

Asked

Viewed 32 times

0

HTML

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Document</title>
</head>
<body>
   <input type="text" id="entrada" placeholder="Nome">
   <input type="button" value = "Iniciar"onclick = "botao()">

   <input type="text" id="entrada2" placeholder="nota">
 <ul id="tabela">
     <li id="NotaMaior"></li>
     <li id="NotaMenor"></li>
 </ul>
   <script src="JavaScript.js"></script>
</body>
</html>

Javascript

"use strict"
let maior = 0
let menor = 0
function botao() {
    tabela.innerHTML +=
        `<tr>
             <td> ${entrada.value} </td>
             <td> ${entrada2.value}</td>
         </tr>`
    if (entrada2.value > maior) {
        maior = entrada2.value
    }
    else if ((entrada2.value < menor) || (menor == 0)) {
        menor = entrada2.value
    }
    NotaMaior.innerHTML += ` / Maior ${maior}`
    NotaMenor.innerHTML += ` / Menor ${menor}`
}

I’m having trouble showing the biggest and the smallest, could you help me ? Grateful!

1 answer

1


This is a typing problem and is happening because the property value of Javascript returns a string. You will not have problems when you run the code for the first time because it will compare string with a number. However, it will save its (larger) result as string and there’s the problem. When you try to compare two string it will always compare only the first digit of both string. To fix the problem just turn your string in a number.

  • Fixed one of my problems, but now I have another problem. I added the following line in my code javascript&#xA;if(Number(maior) < Number(menor)){&#xA;menor = maior&#xA;}&#xA; The smallest only takes a value less than 3, if just after 3 I type for example 1. the smallest were taking the smallest results and showing on the screen. Getting like this : larger : 1 // greater : 2 // greater : 3 . minor : 0 // minor : 1 // minor: 1 ..

Browser other questions tagged

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