#[TIL] #11 JavaScript - 가위바위보

DamHo Bae·2021년 2월 18일
0

가위바위보 게임 만들기

A와 B가 가위바위보를 하는 게임이다.
ex) a가 승리시 리턴값 a / 무승부시 a리턴
a를 player1로 지정 b는 player2 지정
||연산자와 && 사용 해야한다.

경우의 수 부터 생각해야한다.

a:

  • 가위
  • 바위

  • b:
  • 가위
  • 바위

a에 경우 무승부까지 생각하면
player1 === "가위" && player2 === "보"
player1 === "가위" && player2 === "가위"
player1 === "보" && player2 === "바위"
player1 === "보" && player2 === "보"
player1 === "바위" && player2 === "가위"
player1 === "바위" && player2 === "바위"

if문에서 총 a는 6줄
else문에서 총 3줄

|| && 연산자 사용

function rockPaperScissors(player1, player2) {
if((player1 === "가위" && player2 === "보")||
   (player1 === "가위" && player2 === "가위")||
   (player1 === "보" && player2 === "바위")||
   (player1 === "보" && player2 === "보") ||
   (player1 === "바위" && player2 === "가위")||
   (player1 === "바위" && player2 === "바위")
){return "player1";}
else if
((player1 === "가위" && player2 === "바위")
||(player1 === "바위" && player2 === "보")
||(player1 === "보" && player2 === "가위")
){ return "player2";}
}

최대한 코드를 줄여봤다. 처음에 감을 잡기 어려웠지만 우영님에게 도움을 받고
경우의 수를 알려주셨다! 감사합니다 우영님 : )

profile
Frontend Developer

0개의 댓글