-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
154 lines (123 loc) · 4.86 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Challenge 1: Your age in days
function ageInDays(){
var birthYear = prompt('What year were you born... Good Friend?');
var ageInDayss = (2021-birthYear) * 365;
var h1 = document.createElement('h1');
var textAnswers = document.createTextNode('You are ' + ageInDayss + ' days old.');
h1.setAttribute('id', 'ageInDays');
h1.appendChild(textAnswers);
document.getElementById('flex-box-result').appendChild(h1);
}
function reset() {
document.getElementById('ageInDays').remove();
}
// Challenge 2: Generate Cat
function generateCat() {
var image = document.createElement('img');
var div = document.getElementById('flex-cat-gen');
image.src = "https://source.unsplash.com/user/erondu/200x200";
div.appendChild(image);
}
// Challenge 3: Rock, Paper, Scissors
function rpsGame(yourChoice) {
console.log(yourChoice);
var humanChoice, botChoice;
humanChoice = yourChoice.id;
botChoice = numberToChoice(randToRpsInt());
console.log('computerChoice',botChoice);
results = decideWinner(humanChoice,botChoice);
console.log(results);
message = finalMessage(results);
console.log(message);
// {'message': 'You won!','color': 'green'}
rpsFrontEnd(yourChoice.id,botChoice,message);
}
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==1){
return {'message': 'You won!','color': 'green'};
} else{
return {'message': 'You tied!','color': 'blue'};
}
}
function rpsFrontEnd(humanImageChoice, botImageChoice, finalMessage){
var imageDatabase = {
'rock': document.getElementById('rock').src,
'paper': document.getElementById('paper').src,
'scissors': document.getElementById('scissors').src
}
// lets remove images
document.getElementById('rock').remove();
document.getElementById('paper').remove();
document.getElementById('scissors').remove();
var humanDiv = document.createElement('div');
var botDiv = document.createElement('div');
var messageDiv = document.createElement('div');
humanDiv.innerHTML = "<img src='" +imageDatabase[humanImageChoice]+"'height = 150 width = 150 >"
messageDiv.innerHTML = "<h1 style='color: "+ finalMessage['color']+"; font-size:60px; padding: 30px;'>" + finalMessage['message']+ "</h1>"
botDiv.innerHTML = "<img src='" +imageDatabase[botImageChoice]+"'height = 150 width = 150 >"
document.getElementById('flex-box-rps-div').appendChild(humanDiv);
document.getElementById('flex-box-rps-div').appendChild(messageDiv);
document.getElementById('flex-box-rps-div').appendChild(botDiv);
}
// Challenge 4: Change the colour of all button
var all_buttons = document.getElementsByTagName('button');
var copyAllButtons = [];
for (let i = 0; i < all_buttons.length; i++){
copyAllButtons.push(all_buttons[i].classList[1]);
}
console.log(copyAllButtons);
function buttonColorChange(buttonThingy){
if(buttonThingy.value =='red'){
buttonRed();
} else if(buttonThingy.value =='green'){
buttonGreen();
} else if(buttonThingy.value =='reset'){
buttonColorReset();
} else if(buttonThingy.value =='random'){
randomColors();
}
}
function buttonRed(){
for(let i = 0; i<all_buttons.length; i++){
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add('btn-danger');
}
}
function buttonGreen(){
for(let i = 0; i<all_buttons.length; i++){
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add('btn-success');
}
}
function buttonColorReset(){
for(let i = 0; i<all_buttons.length; i++){
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add(copyAllButtons[i]);
}
}
function randomColors(){
let choices = ['btn-primary', 'btn-danger', 'btn-success','btn-warning']
for(let i = 0; i<all_buttons.length; i++){
var randomNumber = Math.floor(Math.random()*4);
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add(choices[randomNumber]);
}
}