[프로그래머스 lv.0] 홀짝 구분하기

username_oy·2023년 6월 22일

프로그래머스

목록 보기
4/4

📌문제설명

자연수 n이 입력으로 주어졌을 때, 만약 n이 짝수면 "n is even"을, 홀수이면 "n is odd"를 출력하는 코드를 작성해 보세요.


❗ 제한사항

  • 1 ≤ n ≤ 1000

입출력 예

입력 #1

100

출력 #1

100 is even

입력 #2

1

출력 #2

1 is odd

💭나의 생각

문제를 보고 조건식을 사용해 문제를 풀었다.
문제는 풀었지만 조건 연산자로 대체 가능하다는 생각은 못했다.
조금 더 간결한 코드를 위해서 조건 연산자로 대체 하는 것이 좋을 듯 하다.

조건 연산자

condition ? exprIfTrue : exprIfFalse
// 조건문 ? 조건문이 참일 때 : 조건문이 거짓일 때

✍️풀이


const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

let input = [];

rl.on('line', function (line) {
  input = line.split(' ');
}).on('close', function () {
  n = Number(input[0]);
  if (n % 2 == 0) {
    console.log(`${n} is even`);
  } else {
    console.log(`${n} is odd`);
  }
});

let input = [];

rl.on('line', function (line) {
  input = line.split(' ');
}).on('close', function () {
  n = Number(input[0]);
  console.log( n % 2 === 0 ? `${n} is even` : `${n} is odd`)
profile
프런트엔드 개발자로의 여정

0개의 댓글