-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (48 loc) · 1.57 KB
/
script.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
//1.Compare two JSON have the same properties without order:
// let obj1 = {name: "Person 1", age: 5};
// let obj2 = {age: 5, name: "Person 1"};
function isEqual(obj1, obj2) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (let key of keys1) {
if (!keys2.includes(key) || obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}
let obj1 = {name: "Person 1", age: 5};
let obj2 = {age: 5, name: "Person 1"};
console.log(isEqual(obj1, obj2)); // Output: true
//-------------------------------------------------------------------------
//2.XMLHttpRequest
//Print all country flags
var request = new XMLHttpRequest;
request.open("GET","https://restcountries.com/v3.1/all",true);
request.send()
request.onload = function()
{
var data = request.response;
var result = JSON.parse(data)
for(var i=0;i<result.length;i++)
{
console.log(result[i].flags.png);
}
};
//------------------------------------------------------------------------
//3.Print all countries names, region, subregion and populations
var request = new XMLHttpRequest;
request.open("GET","https://restcountries.com/v3.1/all",true);
request.send()
request.onload = function()
{
var data = request.response;
var result = JSON.parse(data)
for(var i=0;i<result.length;i++)
{
console.log("countryname:"+" "+result[i].name.common,"Region:"+" "+result[i].region,"subregion"+" "+result[i].subregion,"population"+" "+result[i].population)
}
}