๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

Programmers Lv.1 : ์ง์ˆ˜์™€ ํ™€์ˆ˜

by Youcodein 2022. 11. 3.
728x90
๋ฐ˜์‘ํ˜•

Programmers Lv.1 : ์ง์ˆ˜์™€ ํ™€์ˆ˜

๋ฌธ์ œ : " ์ •์ˆ˜ num์ด ์ง์ˆ˜์ผ ๊ฒฝ์šฐ "Even"์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ "Odd"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. "

์กฐ๊ฑด : num์€ int ๋ฒ”์œ„์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. / 0์€ ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‚ด ํ’€์ด

function solution(num) {
  var answer = '';
  if(num % 2 == 0){
      answer = "Even";
  } else {
      answer = "Odd";
  }
  return answer;
}

์ˆ˜์—…์‹œ๊ฐ„์— ๋ฐฐ์› ๋˜ ์ง์ˆ˜( % : ๋‚˜๋จธ์ง€์˜ ๊ฐ’์ด 0)์™€ ํ™€์ˆ˜(์ง์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค)๋ฅผ ํ†ตํ•ด if๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ต์„ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด

function evenOrOdd(num) {
  return num % 2 ? "Odd" : "Even";
}

์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ if๋ฌธ์„ ๋” ๋‹จ์กฐ๋กญ๊ฒŒ ๋งŒ๋“ค์–ด ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์ด๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€