Skip to content

Commit 0346495

Browse files
committed
cats all parsed
1 parent 4e1cc33 commit 0346495

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

quiz12.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html lang="en-US">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>JSON: Task 1</title>
6+
<style>
7+
p {
8+
color: purple;
9+
margin: 0.5em 0;
10+
}
11+
12+
* {
13+
box-sizing: border-box;
14+
}
15+
</style>
16+
<!-- <link rel="stylesheet" href="../styles.css" /> -->
17+
</head>
18+
19+
<body>
20+
21+
<section class="preview">
22+
23+
24+
25+
</section>
26+
27+
</body>
28+
<script>
29+
const section = document.querySelector('section');
30+
31+
let para1 = document.createElement('p');
32+
let para2 = document.createElement('p');
33+
let motherInfo = 'The mother cats are called ';
34+
let kittenInfo = "";
35+
const requestURL = 'https://mdn.github.io/learning-area/javascript/oojs/tasks/json/sample.json';
36+
37+
fetch(requestURL)
38+
.then(response => response.text())
39+
.then(text => displayCatInfo(text))
40+
41+
function displayCatInfo(catString) {
42+
let total = 0;
43+
let male = 0;
44+
45+
// Add your code here
46+
const catInfo = JSON.parse(catString);
47+
48+
for(let i = 0; i < catInfo.length; i++){
49+
if(i < catInfo.length - 1){
50+
motherInfo += catInfo[i].name + ', ';
51+
}
52+
else{
53+
motherInfo += 'and ' + catInfo[i].name + '.';
54+
}
55+
56+
for(let j = 0; j < catInfo[i].kittens.length; j++){
57+
total++;
58+
if(catInfo[i].kittens[j].gender == 'm'){
59+
male++;
60+
}
61+
// kittenInfo += catInfo[i].kittens[j].name + ', ';
62+
}
63+
}
64+
kittenInfo += `They have ${total} kittens in total, ${male} male and ${total - male} female.`
65+
// Don't edit the code below here!
66+
67+
para1.textContent = motherInfo;
68+
para2.textContent = kittenInfo;
69+
}
70+
71+
section.appendChild(para1);
72+
section.appendChild(para2);
73+
</script>
74+
75+
</html>

0 commit comments

Comments
 (0)