본문 바로가기
코딩테스트 뿌수기

[백준 : 2753번 node.js] 윤년

by hans-j 2024. 6. 6.

https://www.acmicpc.net/problem/2753

 

연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성

 

해결 과정

 

1. 주어진 조건 나열

_year % 4 === 0 && _year % 100 !== 0) || _year % 400

 

2. 출력되어야 할 값 추가 

_year % 4 === 0 && _year % 100 !== 0) || _year % 400 === 0 ? 1 : 0;

 

3. 함수로 작성

function leapYearGenerator(_year){
    return (_year % 4 === 0 && _year % 100 !== 0) || _year % 400 === 0 ? 1 : 0;
}

 

4. 테스트 코드 작성

const assert = require('assert');

function leapYearGenerator(_year){
    return (_year % 4 === 0 && _year % 100 !== 0) || _year % 400 === 0 ? 1 : 0;
}

// Test cases
assert.strictEqual(leapYearGenerator(2012), 1); // 2012 is a leap year
assert.strictEqual(leapYearGenerator(1900), 0); // 1900 is not a leap year
assert.strictEqual(leapYearGenerator(2000), 1); // 2000 is a leap year
assert.strictEqual(leapYearGenerator(2021), 0); // 2021 is not a leap year

console.log('All tests passed!');

 

5.제출

const fs = require('fs');
const filePath = process.platform === "linux" ? '/dev/stdin' : __dirname + '/input.txt';
const input = Number(fs.readFileSync(filePath).toString().trim());
let result = input % 4 === 0 && (input % 100 !== 0 || input % 400 === 0 )? 1 : 0;
console.log(result);