-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (76 loc) · 3.32 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
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
//Question 01
console.log("question 01: Compare two JSON have the same properties without order")
//question 01: Compare two JSON have the same properties without order.
//a. let obj1 = {name: "Person1", age:5};
//b. let obj2 = {age:5, name: "Person2"};
let obj1 = {name:"Person 1",age:5};
let obj2 = {age:5,name:"Person 1"};
//we cannot compare two objects straight away here. So, convert these into strings first and then compare them
if(JSON.stringify(obj1) === JSON.stringify(obj2))
{
console.log("answer : "+"Two JSON have the same properties");
}
else
{
console.log("answer : "+"Two JSON do not have the same properties");
}
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("----------------")
//-----------------------------------------
// //question 02
// console.log("question 02: Use the rest countries' API URL -> https://restcountries.com/v3.1/all and display all the country flags in the console")
//create XMLHTTP Request
//step 01 (Object to be sent from Client side)
var myrequest01 = new XMLHttpRequest();
//step 02 (request to server side using the object we created in step 01)
myrequest01.open("GET","https://restcountries.com/v3.1/all",true)
//step 03 (Creating a bridge to server to send our request in step 02)
myrequest01.send();
//step 04 () Server Response
myrequest01.onload = function()
{
var data01 = myrequest01.response;
// console.log(data)
var result01 = JSON.parse(data01); //this parse will load data nicely
//how to access multiple values? for loop
console.log("question 02: Use the rest countries' API URL -> https://restcountries.com/v3.1/all and display all the country flags in the console")
console.log("Answer :");
for(var i=0; i<result01.length; i++)
{
console.log(result01[i].name.common+" Flag: "+result01[i].flags.png)
}
}
//------------------------------------------
// //------------------------------------------
// //question 03: Use the same rest countries and print all countries names, regions, sub-region and populations
//create XMLHTTP Request
//step 01 (Object to be sent from Client side)
var myrequest02 = new XMLHttpRequest();
//step 02 (request to server side using the object we created in step 01)
myrequest02.open("GET","https://restcountries.com/v3.1/all",true)
//step 03 (Creating a bridge to server to send our request in step 02)
myrequest02.send();
//step 04 () Server Response
myrequest02.onload = function()
{
var data02 = myrequest02.response;
// console.log(data)
var result02 = JSON.parse(data02); //this parse will load data nicely
//how to access multiple values? for loop
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("----------------")
console.log("question 03: Use the same rest countries and print all countries names, regions, sub-region and populations")
console.log("Answer :");
for(var i=0; i<result02.length; i++)
{
console.log("country Name: "+result02[i].name.common+" ; Region : "+result02[i].region+" ; Sub-Region : "+result02[i].subregion+" ; Population : "+result02[i].population)
}
}