-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (116 loc) · 4.53 KB
/
Copy pathscript.js
File metadata and controls
147 lines (116 loc) · 4.53 KB
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
//Challange 1
function ageInDays() {
var birthYear = prompt('What yer were you born?');
var ageInDayss = (2020 - birthYear) * 365
var h1 = document.createElement('h1');
var textAnswer = document.createTextNode('You are ' + ageInDayss + 'days old');
h1.setAttribute('id', 'ageinDays');
h1.appendChild(textAnswer);
document.getElementById('flex-box-result').appendChild(h1);
}
function reset() {
document.getElementById('ageinDays').remove();
}
//Challange 2
function generateCat() {
var image = document.createElement('img');
image.src = 'pictures/cat1.jpg';
var div = document.getElementById('flex-cat-gen').appendChild(image);
}
//Challange 3
function rpsGame(yourChoice) {
console.log(yourChoice);
var humanChoice, botChoice;
humanChoice = yourChoice.id;
botChoice = numberToChoice(randomToRpsInt());
results = decideWinner(humanChoice, botChoice);
message = finalMessage(results);
rpsFrontEnd(humanChoice, botChoice, message);
}
function randomToRpsInt() {
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.5 },
'scissors': { 'paper': 1, 'scissors': 0.5, 'rock': 0 }
};
var yourScore = rpsDatabase[yourChoice][computerChoice];
var computerScore = rpsDatabase[computerChoice][yourChoice];
return yourScore;
}
function finalMessage(yourScore) {
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' };
}
}
function rpsFrontEnd(humanImageChoice, botImageChoice, finalMessage) {
imagesDatabase = {
'rock': document.getElementById('rock').src,
'paper': document.getElementById('paper').src,
'scissors': document.getElementById('scissors').src
};
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='" + imagesDatabase[humanImageChoice] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37,50,233,1);'>"
messageDiv.innerHTML = "<h2 style='color: " + finalMessage['color'] + "; font-size: 60px; padding: 30px;'>" + finalMessage['message'] + "<h2>"
botDiv.innerHTML = "<img src='" + imagesDatabase[botImageChoice] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(243,38,24,1);'>"
document.getElementById('flex-box-rps-div').appendChild(humanDiv);
document.getElementById('flex-box-rps-div').appendChild(messageDiv);
document.getElementById('flex-box-rps-div').appendChild(botDiv);
}
//Challange4
var all_buttons = document.getElementsByTagName('button');
var copyAllButtons = [];
for (let i = 0; i < all_buttons.length; i++) {
copyAllButtons.push(all_buttons[i].classList[1]);
}
function buttonColorChange(buttonThingy) {
if (buttonThingy.value == 'red') {
buttonsRed();
} else if (buttonThingy.value === 'green') {
buttonsGreen();
} else if (buttonThingy.value === 'reset') {
buttonReset();
} else if (buttonThingy.value === 'random') {
randomColors();
}
}
function buttonsRed() {
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 buttonsGreen() {
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 buttonReset() {
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() {
var 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]);
}
}