[자바스크립트] 코드아카데미 문제풀이 Build "Rock, Paper, Scissors" (6/9)
What if choice1 is rock?
1. 문제
You're doing great! Now we consider the other scenarios. Let's break the problem down a little. What if choice1
is "rock"
? Given choice1
is "rock"
,
a. if choice2 === "scissors"
, then "rock"
wins
b. if choice2 === "paper"
, then "paper"
wins.
How do we structure this? It's a bit different from what we have already seen. We will first have an if
statement. And then the code inside that if
statement will be... another if
statement! 원문링크
2. 풀이
이용자에게 가위바위보를 결정했다고 물어보고 그 대답을 userchoice에 저장합니다.
3. 해답
다음과 같이 입력하면 통과합니다.
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("human: " + userChoice);
console.log("human: " + userChoice);
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function()
{
if (userChoice===computerChoice)
{return "The result is a tie!"}
else if (userChoice === "rock")
{ if(computerChoice===scissors) {return "rock wins"}
else {return "paer wins"}
}
}
반응형