-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (56 loc) · 1.97 KB
/
script.js
File metadata and controls
68 lines (56 loc) · 1.97 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
/* TASK-1 compare two JSON having same properties wihout order
Given code: a. let obj1 = {name: "person1" , age: 5};
b. let obj1 = {age: 5 , name: "person1"};age: 5 , name: "person1"
*/
let obj1 = {
name: "person1",
age: 5
};
let obj2 = {
age: 5,
name: "person1"
};
let flag=true;
if(Object.keys(obj1).length==Object.keys(obj2).length){
for(key in obj1) {
if(obj1[key] === obj2[key]) {
continue;
}
else {
flag=false;
break;
}
}
}
else {
flag=false;
}
console.log(`the object is equal: ${flag}`);
/* TASK-2 use the rest countries API's URL-> http://restcountries.com/v3.1/all
and display all the country flags in the console */
//create an object to the XMLHttpRequest class constructor
let xhr = new XMLHttpRequest();
//open a new connection the server
xhr.open('GET' , 'https://restcountries.com/v3.1/all');
xhr.send(); //this will send request to the server
// the load event handles the response from the server
xhr.onload = function(){
let countries = JSON.parse(xhr.responseText);
for ( let flags of countries ){
console.log(flags.flag);
}
}
/* TASK-3 use the rest countries API's URL-> http://restcountries.com/v3.1/all
and display all countries name , region , subregion and population */
//create an object to the XMLHttpRequest class constructor
let xhr1 = new XMLHttpRequest();
//open a new connection the server
xhr1.open('GET' , 'https://restcountries.com/v3.1/all');
xhr1.send(); //this will send request to the server
// the load event handles the response from the server
xhr1.onload = function(){
let countries1 = JSON.parse(xhr1.responseText);
for ( let info of countries1 ){
console.log('Country Name:'+' '+info.name.common+ ',', 'Region:'+' '+info.region + ',','SubRegion:'+' '+info.subregion + ',', 'Population:'+' '+info.population);
}
}