Where is the error in the leap year code?

Asked

Viewed 144 times

4

I’m wondering where I went wrong in this code where the user types a year and appears on the screen whether that year is leap or not.

function calcularBissexto() {
  var bi = parseFloat(document.getElementById('bissexto').value);
  var msg = document.getElementById('msg');

  for (bi % 4; bi % 100; bi % 400) {
    if (bi % 4 == 0 || bi % 100 == 0 || bi % 400 == 0) {
      msg.innerHTML += 'E bissexto'
    } else {
      msg.innerHTML += 'Nao e bissexto';
    }
  }
}
<input type="number" id="bissexto">
<button onclick="calcularBissexto()">Click</button>
<div id="msg"></div>

2 answers

6


That one for does not make sense from the point of view of syntax or logic, also does not make sense to give parseFloat() next year, something like this:

function calcularBissexto(ano) {
    return ano % 4 == 0 && ano % 100 != 0 || ano % 400 == 0 ? 'E bissexto' : 'Nao e bissexto';
}
console.log(calcularBissexto(1980));
console.log(calcularBissexto(1981));
console.log(calcularBissexto(1900));
console.log(calcularBissexto(2000));

I put in the Github for future reference.

Note that I took out the logic updating the page, get used to doing functions that are responsible for only one task, this calculates the leap, other should take care of the page update that nor should I wear innerHTML.

3

I know it was not mentioned, however Voce can also validate whether a year is leap or not with the days check of the month. For example:

function calculaAno(){


const getAno = document.getElementById('entradaAno').value;


const AnoBissexto = (ano) => new Date(ano, 1, 29).getMonth() == 1 ? 'bissexto': 'não é bissexto';

const tipoAno = AnoBissexto(getAno);


document.getElementsByClassName('info')[0].innerText = tipoAno;

}
form {
 font-family:sans-serif;
 margin-top:1rem;
 margin-left:1rem;
 margin-right:1rem;
}

input.form-control {
 width:200px;
    
 }

div.info { 
 margin-top:1rem;
 text-align:center;
 }
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>

<form action="">
<label>Digite o ano : </label>
 <input id="entradaAno" type="text" class="form-control" placeholder="" aria-label="" aria-describedby="basic-addon1">
 
 <div class="info alert alert-link" role="alert">
  Por favor digite o ano válido.
</div>

<button type="button" onclick="calculaAno()" class="btn btn-link">Calcular</button>
</form>

</body>

</html>

  • 1

    your code not worked. You used January on Date and not consider Culture. And Why are you using const? I tested your code and got errors.

  • First, the months go from 0 to 11, second and a constant value , know whether it is or not, therefore const

  • I asked because I used your code and tested with the year 2016 and it went bad (2016 is bisext), but when I switched to (year, 2, 29) it worked (with some changes, because the way it is, in practice it went wrong, and is displaying your code instead of the message) . It would be nice if you put up a snippet to show in practice how to use .

Browser other questions tagged

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