I am following a tutorial on YouTube about building projects in Javascript. And this piece of code is supposed to create a Rock, Paper & Scissors game. I understand most part of this code. But my challenge is how the decideWinner function works. How does the variable yourScore pass the value into the rpsDatabase ? and how are the functions being called ?
//Rock, Paper, Scissors
function rpsGame(yourChoice) {
var humanChoice, botChoice, results, message;
humanChoice = yourChoice.id;
botChoice = numberToChoice(randToRpsInt());
results = decideWinner(humanChoice, botChoice);
console.log(botChoice);
}
function randToRpsInt () {
return Math.floor(Math.random() * 3);
}
function numberToChoice (number) {
return ['rock', 'paper', 'scissors'][number];
}
function decideWinner (yourChoice, computerChoice) {
var rpsDatabase = {
'rock': {'scissors': 1, 'rock': 0.5, 'paper': 0},
'paper': {'rock': 1, 'paper': 0.5, 'scissors': 0},
'scissors': {'paper': 1, 'scissors': 0.5, 'rock': 0},
};
var yourScore = rpsDatabase[yourChoice][computerChoice];
var computerScore = rpsDatabase[computerChoice][yourChoice];
return [yourScore, computerScore];
}
function finalMessage([yourScore, computerScore]) {
if (yourScore === 0) {
return {'message': 'You lost!', 'color': 'red'};
} else if (yourScore === 0.5) {
return {'message': 'You tied!', 'color': 'yellow'};
} else {
return {'message': 'You won!', 'color': 'green'};
}
}
.container-game {
border: 1px solid black;
width: 75%;
margin: 0 auto;
text-align: center;
}
.flex-box-weapons {
display: flex;
border: 1px solid black;
padding: 10px;
flex-wrap: wrap;
justify-content: space-around;
}
.flex-box-weapons img:hover {
box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="content/css/style.css">
<title>RPS</title>
</head>
<body>
<div class="container-game">
<h2>Challenge 3: Rock, Paper, Scissors</h2>
<div class="flex-box-weapons" id="flex-box-weapons-div">
<img id="Rock" alt="Rock" src="http://images.clipartpanda.com/rock-clipart-clipart-harvestable-resources-rock.png" width="150" height="150" onclick="rpsGame(this)">
<img id="Paper" alt="Paper" src="
http://images.clipartpanda.com/paper-clipart-nexxuz-Loose-Leaf-Paper.png" width="150" height="150" onclick="rpsGame(this)">
<img id="Scissors" alt="Scissors" src="https://thumbs.dreamstime.com/b/female-hand-sign-victory-sign-peace-sing-scissors-vector-color-flat-illustration-isolated-white-background-web-83128345.jpg" width="150" height="150" onclick="rpsGame(this)">
</div>
</div>
<script src="content/Js/script.js">
</script>
</body>
</html>