-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (73 loc) · 2.3 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
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
//Q1 How to compare two JSON have the same properties without order
// a.let Obj1 = {name:"person 1", age:5};
// b.let Obj2 = {age:5, name: "person 1"};
let Obj1 = {name:"person 1", age:5}
var Obj2 = {age:5, name: "person 1"}
let strObj1 = JSON.stringify(Obj1);
let strObj2 = JSON.stringify(Obj2);
if (JSON.stringify(Obj1) === JSON.stringify(Obj2))
{
console.log("both are equal");
}
else
{
console.log("both are different");
}
//Q2 Use the rest countries API URL => https://restcountries.eu/rest/v2/all and display
// all the country flags in the console
var request = new XMLHttpRequest();
request.open("GET","https://restcountries.com/v3.1/all",true)
request.send();
request.onload = function (){
var value = request.response;
var result = JSON.parse(value);
console.log(result);
for (var i =0;i<result.length;i++){
console.log(result[i].flags.png);
}
}
//Q3 Use the same rest countries and print all countries names,regions,sub regions and populations
var request1 = new XMLHttpRequest();
request1.open("GET","https://restcountries.com/v3.1/all",true)
request1.send();
request1.onload = function (){
var data1 = request1.response;
var result1 = JSON.parse(data1);
console.log(result1);
for (var i =0;i<result1.length;i++){
console.log(result1[i].name.common);
}
}
var request2 = new XMLHttpRequest();
request2.open("GET","https://restcountries.com/v3.1/all",true)
request2.send();
request2.onload = function (){
var data2 = request2.response;
var result2 = JSON.parse(data2);
console.log(result2);
for (var i =0;i<result2.length;i++){
console.log(result2[i].region);
}
}
var request3 = new XMLHttpRequest();
request3.open("GET","https://restcountries.com/v3.1/all",true)
request3.send();
request3.onload = function (){
var data3 = request1.response;
var result3 = JSON.parse(data3);
console.log(result3);
for (var i =0;i<result3.length;i++){
console.log(result3[i].subregion);
}
}
var request4 = new XMLHttpRequest();
request4.open("GET","https://restcountries.com/v3.1/all",true)
request4.send();
request4.onload = function (){
var data4 = request1.response;
var result4 = JSON.parse(data4);
console.log(result4);
for (var i =0;i<result4.length;i++){
console.log(result4[i].population);
}
}