Skip to content

Commit 971cffe

Browse files
committed
update
1 parent 74d859c commit 971cffe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const btn = document.querySelector('.btn');
2+
const content = document.querySelector('.content');
3+
const URL = 'https://api.chucknorris.io/jokes/random';
4+
5+
btn.addEventListener('click', () => {
6+
getData(URL);
7+
});
8+
9+
function getData(url) {
10+
const xhr = new XMLHttpRequest();
11+
xhr.open('GET', url);
12+
xhr.send();
13+
xhr.onreadystatechange = function () {
14+
if (xhr.readyState !== 4) return;
15+
if (xhr.status === 200) {
16+
const { value: joke } = JSON.parse(xhr.responseText);
17+
content.textContent = joke;
18+
} else {
19+
console.log({
20+
status: xhr.status,
21+
text: xhr.statusText,
22+
});
23+
}
24+
};
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const btn = document.querySelector('.btn');
2+
const content = document.querySelector('.content');
3+
const img = document.querySelector('.container img');
4+
const URL = 'https://api.chucknorris.io/jokes/random';
5+
6+
btn.addEventListener('click', () => {
7+
getData(URL);
8+
});
9+
10+
function getData(url) {
11+
const xhr = new XMLHttpRequest();
12+
xhr.open('GET', url);
13+
xhr.send();
14+
xhr.onreadystatechange = function () {
15+
if (xhr.readyState !== 4) return;
16+
if (xhr.status === 200) {
17+
img.classList.add('shake-img');
18+
const { value: joke } = JSON.parse(xhr.responseText);
19+
content.textContent = joke;
20+
const random = Math.random() * 1000;
21+
setTimeout(() => {
22+
img.classList.remove('shake-img');
23+
}, random);
24+
} else {
25+
console.log({
26+
status: xhr.status,
27+
text: xhr.statusText,
28+
});
29+
}
30+
};
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const btn = document.querySelector('.btn');
2+
const content = document.querySelector('.content');
3+
const img = document.querySelector('.container img');
4+
const URL = 'https://api.chucknorris.io/jokes/random';
5+
6+
btn.addEventListener('click', () => {
7+
getData(URL)
8+
.then((response) => displayData(response))
9+
.catch((err) => console.log(err));
10+
});
11+
12+
function getData(url) {
13+
return new Promise((resolve, reject) => {
14+
const xhr = new XMLHttpRequest();
15+
xhr.open('GET', url);
16+
xhr.send();
17+
xhr.onreadystatechange = function () {
18+
if (xhr.readyState !== 4) return;
19+
if (xhr.status === 200) {
20+
resolve(xhr.responseText);
21+
} else {
22+
reject({
23+
status: xhr.status,
24+
text: xhr.statusText,
25+
});
26+
}
27+
};
28+
});
29+
}
30+
31+
function displayData(data) {
32+
img.classList.add('shake-img');
33+
const { value: joke } = JSON.parse(data);
34+
content.textContent = joke;
35+
const random = Math.random() * 1000;
36+
setTimeout(() => {
37+
img.classList.remove('shake-img');
38+
}, random);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const btn = document.querySelector('.btn');
2+
const content = document.querySelector('.content');
3+
const img = document.querySelector('.container img');
4+
const URL = 'https://api.chucknorris.io/jokes/random';
5+
6+
btn.addEventListener('click', () => {
7+
fetch(URL)
8+
.then((data) => data.json())
9+
.then((response) => displayData(response))
10+
.catch((err) => console.log(err));
11+
});
12+
13+
function displayData({ value: joke }) {
14+
img.classList.add('shake-img');
15+
// const { value: joke } = data;
16+
content.textContent = joke;
17+
const random = Math.random() * 1000;
18+
setTimeout(() => {
19+
img.classList.remove('shake-img');
20+
}, random);
21+
}

AJAX_Projects/1-chuck-norris-jokes/final/5-async-await.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const btn = document.querySelector('.btn');
2+
const content = document.querySelector('.content');
3+
const img = document.querySelector('.container img');
4+
const URL = 'https://api.chucknorris.io/jokes/random';
5+
6+
btn.addEventListener('click', async () => {
7+
try {
8+
const data = await fetch(URL);
9+
const response = await data.json();
10+
displayData(response);
11+
} catch (error) {
12+
console.log(error);
13+
}
14+
});
15+
16+
function displayData({ value: joke }) {
17+
img.classList.add('shake-img');
18+
// const { value: joke } = data;
19+
content.textContent = joke;
20+
const random = Math.random() * 1000;
21+
setTimeout(() => {
22+
img.classList.remove('shake-img');
23+
}, random);
24+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Random Jokes</title>
7+
</head>
8+
<link rel="stylesheet" href="./styles.css" />
9+
<body>
10+
<main>
11+
<div class="container">
12+
<img src="./chuck.png" alt="chuck norris" />
13+
<button class="btn" type="button">
14+
make me laugh chuck !
15+
</button>
16+
<p class="content"></p>
17+
</div>
18+
</main>
19+
<script src="./app.js"></script>
20+
</body>
21+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
===============
3+
Fonts
4+
===============
5+
*/
6+
@import url('https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap');
7+
8+
/*
9+
===============
10+
Variables
11+
===============
12+
*/
13+
14+
:root {
15+
/* dark shades of primary color*/
16+
--clr-primary-1: hsl(205, 86%, 17%);
17+
--clr-primary-2: hsl(205, 77%, 27%);
18+
--clr-primary-3: hsl(205, 72%, 37%);
19+
--clr-primary-4: hsl(205, 63%, 48%);
20+
/* primary/main color */
21+
--clr-primary-5: #49a6e9;
22+
/* lighter shades of primary color */
23+
--clr-primary-6: hsl(205, 89%, 70%);
24+
--clr-primary-7: hsl(205, 90%, 76%);
25+
--clr-primary-8: hsl(205, 86%, 81%);
26+
--clr-primary-9: hsl(205, 90%, 88%);
27+
--clr-primary-10: hsl(205, 100%, 96%);
28+
/* darkest grey - used for headings */
29+
--clr-grey-1: hsl(209, 61%, 16%);
30+
--clr-grey-2: hsl(211, 39%, 23%);
31+
--clr-grey-3: hsl(209, 34%, 30%);
32+
--clr-grey-4: hsl(209, 28%, 39%);
33+
/* grey used for paragraphs */
34+
--clr-grey-5: hsl(210, 22%, 49%);
35+
--clr-grey-6: hsl(209, 23%, 60%);
36+
--clr-grey-7: hsl(211, 27%, 70%);
37+
--clr-grey-8: hsl(210, 31%, 80%);
38+
--clr-grey-9: hsl(212, 33%, 89%);
39+
--clr-grey-10: hsl(210, 36%, 96%);
40+
--clr-white: #fff;
41+
--clr-red-dark: hsl(360, 67%, 44%);
42+
--clr-red-light: hsl(360, 71%, 66%);
43+
--clr-green-dark: hsl(125, 67%, 44%);
44+
--clr-green-light: hsl(125, 71%, 66%);
45+
--clr-black: #222;
46+
--ff-primary: 'Roboto', sans-serif;
47+
--ff-secondary: 'Open Sans', sans-serif;
48+
--transition: all 0.3s linear;
49+
--spacing: 0.25rem;
50+
--radius: 0.5rem;
51+
--light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
52+
--dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
53+
--max-width: 1170px;
54+
--fixed-width: 620px;
55+
}
56+
/*
57+
===============
58+
Global Styles
59+
===============
60+
*/
61+
62+
*,
63+
::after,
64+
::before {
65+
margin: 0;
66+
padding: 0;
67+
box-sizing: border-box;
68+
}
69+
body {
70+
font-family: var(--ff-secondary);
71+
background: var(--clr-grey-10);
72+
color: var(--clr-grey-1);
73+
line-height: 1.5;
74+
font-size: 0.875rem;
75+
}
76+
ul {
77+
list-style-type: none;
78+
}
79+
a {
80+
text-decoration: none;
81+
}
82+
83+
h1,
84+
h2,
85+
h3,
86+
h4 {
87+
letter-spacing: var(--spacing);
88+
text-transform: capitalize;
89+
line-height: 1.25;
90+
margin-bottom: 0.75rem;
91+
font-family: var(--ff-primary);
92+
}
93+
h1 {
94+
font-size: 3rem;
95+
}
96+
h2 {
97+
font-size: 2rem;
98+
}
99+
h3 {
100+
font-size: 1.25rem;
101+
}
102+
h4 {
103+
font-size: 0.875rem;
104+
}
105+
p {
106+
margin-bottom: 1.25rem;
107+
color: var(--clr-grey-5);
108+
}
109+
@media screen and (min-width: 800px) {
110+
h1 {
111+
font-size: 4rem;
112+
}
113+
h2 {
114+
font-size: 2.5rem;
115+
}
116+
h3 {
117+
font-size: 1.75rem;
118+
}
119+
h4 {
120+
font-size: 1rem;
121+
}
122+
body {
123+
font-size: 1rem;
124+
}
125+
h1,
126+
h2,
127+
h3,
128+
h4 {
129+
line-height: 1;
130+
}
131+
}
132+
/* global classes */
133+
134+
.btn {
135+
text-transform: uppercase;
136+
background: var(--clr-primary-5);
137+
color: var(--clr-white);
138+
padding: 0.375rem 0.75rem;
139+
letter-spacing: var(--spacing);
140+
display: block;
141+
transition: var(--transition);
142+
font-size: 0.875rem;
143+
border: 2px solid var(--clr-primary-5);
144+
cursor: pointer;
145+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
146+
border-radius: var(--radius);
147+
}
148+
.btn:hover {
149+
color: var(--clr-primary-5);
150+
background: var(--clr-primary-1);
151+
border-color: var(--clr-primary-1);
152+
}
153+
/* section */
154+
.section {
155+
padding: 5rem 0;
156+
}
157+
158+
.section-center {
159+
width: 90vw;
160+
margin: 0 auto;
161+
max-width: 1170px;
162+
}
163+
@media screen and (min-width: 992px) {
164+
.section-center {
165+
width: 95vw;
166+
}
167+
}
168+
@keyframes shake {
169+
0% {
170+
transform: translate(1px, 1px) rotate(0deg);
171+
}
172+
10% {
173+
transform: translate(-1px, -2px) rotate(-1deg);
174+
}
175+
20% {
176+
transform: translate(-3px, 0px) rotate(1deg);
177+
}
178+
30% {
179+
transform: translate(3px, 2px) rotate(0deg);
180+
}
181+
40% {
182+
transform: translate(1px, -1px) rotate(1deg);
183+
}
184+
50% {
185+
transform: translate(-1px, 2px) rotate(-1deg);
186+
}
187+
60% {
188+
transform: translate(-3px, 1px) rotate(0deg);
189+
}
190+
70% {
191+
transform: translate(3px, 1px) rotate(-1deg);
192+
}
193+
80% {
194+
transform: translate(-1px, -1px) rotate(1deg);
195+
}
196+
90% {
197+
transform: translate(1px, 2px) rotate(0deg);
198+
}
199+
100% {
200+
transform: translate(1px, -2px) rotate(-1deg);
201+
}
202+
}
203+
/* */
204+
205+
.container {
206+
width: 90vw;
207+
max-width: 60em;
208+
margin: 0 auto;
209+
margin-top: 10rem;
210+
text-align: center;
211+
display: grid;
212+
justify-items: center;
213+
}
214+
img {
215+
display: block;
216+
margin-bottom: 2rem;
217+
}
218+
.content {
219+
margin-top: 2rem;
220+
color: var(--clr-grey-5);
221+
font-size: 1.5rem;
222+
}
223+
.shake-img {
224+
animation: shake 0.25s;
225+
animation-iteration-count: infinite;
226+
}

0 commit comments

Comments
 (0)