-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
126 lines (106 loc) · 4.34 KB
/
main.js
File metadata and controls
126 lines (106 loc) · 4.34 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
//------------------- Assignment 1 -------------------
// create file called articles.json
[
{
"id": "1",
"writer": "Ahmed Ali",
"section": "Technology",
"title": "Latest Developments in Artificial Intelligence in 2025",
"content": "The field of artificial intelligence has seen tremendous advancements in 2025, with models becoming more efficient and responsive. AI has been integrated into various sectors such as healthcare, education, and industry, significantly improving performance and productivity."
},
{
"id": "2",
"writer": "Fatima Mohamed",
"section": "Health",
"title": "The Benefits of Intermittent Fasting for Health",
"content": "Intermittent fasting is one of the best dietary strategies for maintaining good health. It helps regulate blood sugar levels, improve metabolism, and enhance heart health. Studies have also proven its effectiveness in healthy and natural weight loss."
},
{
"id": "3",
"writer": "Khaled Abdel Rahman",
"section": "Economy",
"title": "How Do Cryptocurrencies Affect the Global Economy?",
"content": "Cryptocurrencies like Bitcoin and Ethereum are playing a major role in the global economy, influencing traditional payment systems and investments. With increasing interest in these digital assets, governments are seeking to regulate their use to ensure market stability."
},
{
"id": "4",
"writer": "Mona Ibrahim",
"section": "Self-Development",
"title": "Top Daily Habits for Success",
"content": "Success depends on adopting positive habits such as setting goals, waking up early, practicing mindfulness, and continuously developing skills. Applying these habits can help achieve balance and success in various aspects of life."
},
{
"id": "5",
"writer": "Yasser Hassan",
"section": "Sports",
"title": "How to Stay Fit at Home?",
"content": "You can maintain your fitness without going to the gym by doing home workouts such as resistance exercises, yoga, and cardio. A healthy diet is also recommended to support overall health and fitness."
}
]
//------------------- Assignment 2 -------------------
let request = new XMLHttpRequest();
request.open("GET", "articles.json");
request.send();
console.log(request);
request.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
console.log(this.responseText);
console.log("Data Loaded");
console.log("");
}
};
//------------------- Assignment 3 -------------------
let req = new XMLHttpRequest();
req.open("GET", "articles.json");
req.send();
req.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
let mainData = JSON.parse(this.responseText);
for (let i = 0; i < mainData.length; i++) {
mainData[i].section = "All";
}
console.log(mainData);
let updatedData = JSON.stringify(mainData);
console.log(updatedData);
}
};
//------------------- Assignment 4 -------------------
let request4 = new XMLHttpRequest();
request4.open("GET", "articles.json");
request4.send();
request4.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
let parentDiv = document.createElement("div");
parentDiv.id = "data";
let data = JSON.parse(this.responseText);
for (let i = 0; i < data.length; i++) {
let div = document.createElement("div");
let title = document.createElement("h2");
title.appendChild(document.createTextNode(data[i].title));
div.appendChild(title);
let body = document.createElement("p");
body.appendChild(document.createTextNode(data[i].content));
div.appendChild(body);
let author = document.createElement("p");
author.appendChild(document.createTextNode(`Author: ${data[i].writer}`));
div.appendChild(author);
let category = document.createElement("p");
category.appendChild(
document.createTextNode(`Category: ${data[i].section}`)
);
div.appendChild(category);
parentDiv.appendChild(div);
}
document.body.appendChild(parentDiv);
}
};
// Template Literals Method
// for (let i = 0; i < data.length; i++) {
// let div = `<div>
// <h2>${data[i].title}</h2>
// <p>${data[i].content}</p>
// <p>Author: ${data[i].writer}</p>
// <p>Category: ${data[i].section}</p>
// </div>`;
// parentDiv.innerHTML += div;
// }