[자바스크립트] 코드아카데미 문제풀이 Build "Rock, Paper, Scissors" (5/9)
Both choices are the same!
Now comes the fun part! We need to create a function. It will take two parameters (ie. the two choices made) and then return the winning choice.
When programming a game like this, you have to first figure out all the various outcomes. One outcome is that the choice the user makes is equal to the choice the computer makes. [원문 링크]
컴퓨터와 인간이 낸 가위바위보를 비교하여 같을 경우에 대한 처리를 합니다. if문을 사용해 같을 경우 특정 메시지를 리턴합니다.
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 {return "some one is won!"}
}
위와 같이 입력하면 통과합니다.
반응형