-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4_AIChristmasJokeGenerator.js
192 lines (158 loc) Β· 3.99 KB
/
Day4_AIChristmasJokeGenerator.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
HTML:
<!doctype html>
<html>
<head>
<title>Christmas Joke</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Coiny&family=Poppins:wght@600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="index.css">
</head>
<body>
<main id="window-container">
<section class="left-door">
<img src="left-door-min.png">
</section>
<section class="joke-display">
<h1 id="joke-display">
<span id="joke-question"></span>
<br/>
<span id="joke-answer"></span>
</h1>
</section>
<section class="right-door">
<img src="right-door-min.png">
</section>
</main>
<script src="index.js"></script>
</body>
</html>
CSS:
:root {
--radius: 5px;
--main-frame: 15px solid white;
}
* {
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
}
body {
background-image: url('/xmas-bg3.png');
background-size: cover;
display: flex;
height: 100vh;
align-items: center;
}
h1 {
font-family: Cambria, Cochin, Georgia, Times, Times New Roman, serif;
}
span {
display: inline-block;
margin: 10px;
}
main {
display: flex;
margin: 0 auto;
position: relative;
padding: 0;
}
img {
height: 450px;
display: block;
}
.left-door {
border: var(--main-frame);
border-right: 0;
border-top-left-radius: var(--radius);
border-bottom-left-radius: var(--radius);
}
.left-door img {
border-right: 0;
border-top-left-radius: var(--radius);
border-bottom-left-radius: var(--radius);
}
.right-door {
border: var(--main-frame);
border-left: 0;
border-top-right-radius: var(--radius);
border-bottom-right-radius: var(--radius);
}
.right-door img {
border-left: 0;
border-top-right-radius: var(--radius);
border-bottom-right-radius: var(--radius);
}
.joke-display {
position: absolute;
background-color: rgba(0,0,0,0.1);
opacity: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
border: var(--main-frame);
height: 100%;
padding: 1em;
cursor: pointer;
}
/* animation */
@keyframes display-joke {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes left-open {
0% {
transform: translateX(0%);
}
100% {
transform: translateX(-100%);
}
}
@keyframes right-open {
from {
transform: translateX(0%);
}
to {
transform: translateX(100%);
}
}
*/
/** uncomment one of these **/
// import OpenAI from "openai" --> no
// import { HfInference } from '@huggingface/inference' --> no
/**
* π Challenge:
* 1. When clicked, the doors should open
* to reveal a festive joke.
* π hint.md for help!
**/
/*
API I ended up using: https://sahithyandev.github.io/sv443-joke-api-js-wrapper/
*/
let jokeQuestion = document.getElementById("joke-question")
let jokeAnswer = document.getElementById("joke-answer")
document.getElementById('window-container').addEventListener("click", function () {
fetch("https://v2.jokeapi.dev/joke/Christmas")
.then(response => {
if (!response.ok) {
throw new Error("Oops, try again!");
}
return response.json();
})
.then(data => {
jokeQuestion.innerText = `Q: ${data.setup}` //"setup" is a property of the API
jokeAnswer.innerText = `A: ${data.delivery}` //"delivery" is a property of the API
})
.catch(error => {
console.error('Error:', error);
});
document.querySelector('.left-door').style = "animation: left-open 0.3s forwards"
document.querySelector('.right-door').style = "animation: right-open 0.3s forwards"
document.querySelector('.joke-display').style = "animation: display-joke 0.3s forwards"
})