저장을 습관화
프로그래머스 LV.0 홀짝 구분하기 본문
프로그래머스 LV.0 홀짝 구분하기
https://school.programmers.co.kr/learn/courses/30/lessons/181944
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
홀짝 구분하기
2. 문제 설명
자연수 n이 입력으로 주어졌을 때 만약 n이 짝수이면 "n is even"을, 홀수이면 "n is odd"를 출력하는 코드를 작성해 보세요.
3. 제한 사항
1 ≤ n ≤ 1,000
4. 예시
입력 #1
| 100 | 
출력 #1
| 100 is even | 
입력 #2
| 1 | 
출력 #2
| 1 is odd | 
5. 기본 제공 코드
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]);
});
6. 제출한 내 답
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`);
  }
});
6-2. VSC에 작성한 내용
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`);
  }
});
7. 특이사항
없음
8. 다른 사람이 작성한 답
8-1. 삼항연산자
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', function (line) {
    const result = Number(line) % 2 ? 'odd' : 'even'
    console.log(line, 'is', result)
})
8-2. 삼항연산자 2
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]);
    console.log(n%2===0? `${n} is even`:`${n} is odd`)
});
8-3. 삼항연산자 3
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]);
    let str = n;
    str+= (n%2===0) ? " is even" : " is odd";
    console.log(str);
});'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
| 프로그래머스 LV.1 핸드폰 번호 가리기 (0) | 2023.09.09 | 
|---|---|
| 프로그래머스 LV.0 문자열 겹쳐쓰기 (0) | 2023.09.09 | 
| 프로그래머스 LV.0 문자열 돌리기 (0) | 2023.09.08 | 
| 프로그래머스 LV.0 문자열 붙여서 출력하기 (0) | 2023.09.08 | 
| 프로그래머스 LV.0 덧셈식 출력하기 (0) | 2023.09.08 | 
