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;
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:
See wikipedia: https://en.wikipedia.org/wiki/Leap_year
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')