I’m making a rock, paper, scissors game, but it seems doesn’t work well. I’ve tried looking for mistakes though I can’t find any. When I click the image, it doesn’t pop out as well as the text/message. The console said:
JavaScript
x
5
1
script.js:91 Uncaught ReferenceError: imageDatabase is not defined
2
at rpsFrontEnd (script.js:91)
3
at rpsGame (script.js:39)
4
at HTMLImageElement.onclick (index.html:39)
5
Could you please help me correct them? I really appreciate your help, Thanks.
JavaScript
1
93
93
1
function rpsGame(yourChoice) {
2
console.log(yourChoice);
3
var humanChoice, botChoice;
4
humanChoice = yourChoice.id;
5
6
botChoice = numberToChoice(randToRpsInt());
7
console.log('Computer choice: ', botChoice);
8
9
results = decideWinner(humanChoice, botChoice);
10
console.log(results);
11
12
message = finalMessage(results) // {message: you won, color: green}
13
console.log(message);
14
15
rpsFrontEnd(yourChoice.id, botChoice, message);
16
}
17
18
function randToRpsInt() {
19
return Math.floor(Math.random() * 3);
20
}
21
22
function numberToChoice(number) {
23
return ['rock', 'paper', 'scissors'][number];
24
}
25
26
function decideWinner(yourChoice, computerChoice) {
27
var rpsDatabase = {
28
'rock': {
29
'scissors': 1,
30
'rock': 0.5,
31
'paper': 0
32
},
33
'paper': {
34
'rock': 1,
35
'paper': 0.5,
36
'scissors': 0
37
},
38
'scissors': {
39
'paper': 1,
40
'scissors': 0.5,
41
'rock': 0
42
},
43
}
44
45
var yourScore = rpsDatabase[yourChoice][computerChoice];
46
var computerScore = rpsDatabase[computerChoice][yourChoice];
47
48
return [yourScore, computerScore];
49
}
50
51
function finalMessage([yourScore, computerScore]) {
52
if (yourScore === 0) {
53
return {
54
'message': 'You lost!',
55
'color': 'red'
56
};
57
} else if (yourScore === 0.5) {
58
return {
59
'message': 'You tie!',
60
'color': 'yellow'
61
};
62
} else {
63
return {
64
'message': 'You won!',
65
'color': 'green'
66
};
67
}
68
}
69
70
function rpsFrontEnd(humanImageChoice, botImageChoice, finalMessage) {
71
var imagesDatabase = {
72
'rock': document.getElementById('rock').src,
73
'paper': document.getElementById('paper').src,
74
'scissors': document.getElementById('scissors').src
75
}
76
77
// remove all the images
78
document.getElementById('rock').remove();
79
document.getElementById('paper').remove();
80
document.getElementById('scissors').remove();
81
82
var humanDiv = document.createElement('div');
83
var botDiv = document.createElement('div');
84
var messageDiv = document.createElement('div');
85
86
humanDiv.innerHTML = "<img src='" + imagesDatabase[humanImageChoice] + "' height=150 style='box-shadow: 0px 10px 50px blue;'>"
87
messageDiv.innerHTML = "<h1 style='color: " + finalMessage['color'] + "; font-size: 60px; padding: 30px;'>" + finalMessage['message'] + "</h1>"
88
botDiv.innerHTML = "<img src='" + imageDatabase[botImageChoice] + "' height=150 style='box-shadow: 0px 10px 50px red;'>"
89
90
document.getElementById('flex-box-rps-div').appendChild(humanDiv);
91
document.getElementById('flex-box-rps-div').appendChild(messageDiv);
92
document.getElementById('flex-box-rps-div').appendChild(botDiv);
93
}
JavaScript
1
8
1
<div class="container-3">
2
<h2>Challenge 3: Rock, Paper, Scissors</h2>
3
<div class="flex-box-rps" id="flex-box-rps-div">
4
<img src="https://upload.wikimedia.org/wikipedia/commons/7/7e/Rock-paper-scissors_%28rock%29.png" alt="" id="rock" height=150 onclick="rpsGame(this)">
5
<img src="https://upload.wikimedia.org/wikipedia/commons/a/af/Rock-paper-scissors_%28paper%29.png" alt="" id="paper" height=150 onclick="rpsGame(this)">
6
<img src="https://upload.wikimedia.org/wikipedia/commons/5/5f/Rock-paper-scissors_%28scissors%29.png" alt="" id="scissors" height=150 onclick="rpsGame(this)">
7
</div>
8
</div>
Advertisement
Answer
You have misspelled the imageDatabase, it should be imagesDatabase
JavaScript
1
2
1
botDiv.innerHTML = "<img src='" + imagesDatabase[botImageChoice] + "' height=150 style='box-shadow: 0px 10px 50px red;'>"
2