JavaScript | 자바스크립트 | FUNCTIONS 셋
JavaScript | 자바스크립트 | FUNCTIONS 셋
Build "Rock, Paper, Scissors" | Codecademy
1/9 The Game
File1 _ script.js
2/9 User Chioce
File1 _ script.js
var userChoice = prompt("Do you choose rock, paper or scissors?");3/9 Computer Choice: Part1
File1 _ script.js
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
| 어려워... 문득 배가 점점 산으로 가는 것 같은 느낌임.
4/9 Computer Choice: Part2
File1 _ script.js
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice <= 0.33) {
computerChoice = "rock";
} else if (computerChoice >= 0.67) {
computerChoice = "scissors";
} else {
computerChoice = "paper";
}
5/9 Both choices are the same!
File1 _ script.js
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
};
6/9 What if choice1 is rock?
File1 _ script.js
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
}
};
7/9 What if choice1 is paper?
File1 _ script.js
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
};
8/9 What if choice1 is scissors?
File1 _ script.js
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
| Call your function and pass in userChoice and computerChoice as your two arguments.
이 부분은 compare(); 로 하는 거였군요.
9/9 Next Steps
File1 _ script.js
Next Course: Introduction to 'For' Loops in JS
