-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz12.html
More file actions
75 lines (59 loc) · 1.71 KB
/
quiz12.html
File metadata and controls
75 lines (59 loc) · 1.71 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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8"/>
<title>JSON: Task 1</title>
<style>
p {
color: purple;
margin: 0.5em 0;
}
* {
box-sizing: border-box;
}
</style>
<!-- <link rel="stylesheet" href="../styles.css" /> -->
</head>
<body>
<section class="preview">
</section>
</body>
<script>
const section = document.querySelector('section');
let para1 = document.createElement('p');
let para2 = document.createElement('p');
let motherInfo = 'The mother cats are called ';
let kittenInfo = "";
const requestURL = 'https://mdn.github.io/learning-area/javascript/oojs/tasks/json/sample.json';
fetch(requestURL)
.then(response => response.text())
.then(text => displayCatInfo(text))
function displayCatInfo(catString) {
let total = 0;
let male = 0;
// Add your code here
const catInfo = JSON.parse(catString);
for(let i = 0; i < catInfo.length; i++){
if(i < catInfo.length - 1){
motherInfo += catInfo[i].name + ', ';
}
else{
motherInfo += 'and ' + catInfo[i].name + '.';
}
for(let j = 0; j < catInfo[i].kittens.length; j++){
total++;
if(catInfo[i].kittens[j].gender == 'm'){
male++;
}
// kittenInfo += catInfo[i].kittens[j].name + ', ';
}
}
kittenInfo += `They have ${total} kittens in total, ${male} male and ${total - male} female.`
// Don't edit the code below here!
para1.textContent = motherInfo;
para2.textContent = kittenInfo;
}
section.appendChild(para1);
section.appendChild(para2);
</script>
</html>