Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
react question using javascript leap year in react

trying to figure out how to create a leap year program I created most of it but so far the if statements are not working properly so just trying to figure out why it is not working

import "./App.css";
import leapyear from "./images/leapyear.jpeg";
import React, { useState } from "react";

function App() {
  const [year, setYear] = useState(" ");

  const handleClick = (e) => {
    e.preventDefault();

  if(( 0 == e%4) && ( 0 != e%100 ) && (0 == e%400)){
    console.log('it is a leap year')
  }
  else{
    console.log('it is not a leap year')
  }
}

  return (
    <div className="app">
      <h1>Leap Number Calculator</h1>
      <img className="leapimg" src={leapyear}></img>
      <form onSubmit={handleClick}>
        <label>Enter a year and we will see if it is a leap year</label>
        <input
          type="text"
          value={year}
          onChange={(e) => setYear(e.target.value)}
        ></input>
        <input type="submit"></input>
      </form>
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's not working because your formula for checking if the year is leap is wrong.

function isLeapYear(year) {
  return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
}

To determine if a year is leap, it has to be either:

  • dividable by 400
  • dividable by 4 but not by 100

See wikipedia: https://en.wikipedia.org/wiki/Leap_year

about 4 years ago · Juan Pablo Isaza Relatório

0

const year = 2024

const isLeapYear = (year) => {
  return (year % 100 === 0) ? ( year % 400 === 0) : (year % 4 === 0);
}

if (isLeapyear(year)) console.log('yeah')
if (!isLeapyear(year)) console.log('no')
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda