5
06/11/2024 9:08 오전
주제 스타터
Task: Write a JS function to check whether an year is leap.
Leap years are either divisible by 4 but not by 100 or are divisible by 400.
- The input comes as a single number argument.
- The output should be printed to the console. Print yes if the year is leap and no otherwise.
예시:
입력:
1990
출력:
no
입력:
2000
출력:
yes
입력:
1900
출력:
no
3개 답글
4
06/11/2024 9:09 오전
My solution to the task:
function leapYear(input) { let year = input; let answer; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { answer = "yes"; } else { answer = "no"; } console.log(answer); } leapYear(1999);
3
06/11/2024 9:10 오전
My solution with ternary operator:
function leapYear(year) { let leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); console.log(leap ? "yes" : "no"); } leapYear(2000);
2
06/11/2024 9:11 오전
2 solutions from me:
function isLeapYear(year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) } console.log(isLeapYear(1999));
function checkY(y) { return ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) ? "yes" : "no"; } console.log(checkY(2000));