Skip to content

Commit 9f1700d

Browse files
committed
practice js
1 parent 0079737 commit 9f1700d

2 files changed

Lines changed: 77 additions & 72 deletions

File tree

assesment.js

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,56 @@
11
//promise function
22

3-
4-
let myPromise = new Promise(function(myResolve, myReject) {
5-
let x = 1;
6-
3+
let myPromise = new Promise(function (myResolve, myReject) {
4+
let x = 1;
5+
76
// The producing code (this may take some time)
8-
9-
if (x == 0) {
10-
myResolve("OK");
11-
} else {
12-
myReject("Error");
13-
}
14-
});
15-
16-
myPromise.catch(
17-
function(value) {console.log(value);},
18-
function(error) {console.log(error);}
19-
);
20-
21-
//asynchronous
22-
async function myFunction() {
23-
return "Hello";
24-
}
25-
myFunction().then(
26-
function(value) {console.log(value);},
27-
function(error) {console.log(error);}
28-
);
29-
30-
31-
//await
32-
async function myDisplay() {
33-
let myPromise = new Promise(function(resolve, reject) {
34-
resolve("Kirtti");
35-
});
36-
console.log( await myPromise);
7+
8+
if (x == 0) {
9+
myResolve("OK");
10+
} else {
11+
myReject("Error");
3712
}
38-
39-
myDisplay();
13+
});
4014

41-
//
42-
function myFirst() {
43-
console.log("Kirttinath");
15+
myPromise.catch(
16+
function (value) {
17+
console.log(value);
18+
},
19+
function (error) {
20+
console.log(error);
4421
}
45-
46-
function mySecond() {
47-
console.log("Goodbye");
22+
);
23+
24+
//asynchronous
25+
async function myFunction() {
26+
return "Hello";
27+
}
28+
myFunction().then(
29+
function (value) {
30+
console.log(value);
31+
},
32+
function (error) {
33+
console.log(error);
4834
}
49-
mySecond();//this will execute first
50-
myFirst();//second this is execute
51-
35+
);
36+
37+
//await
38+
async function myDisplay() {
39+
let myPromise = new Promise(function (resolve, reject) {
40+
resolve("Kirtti");
41+
});
42+
console.log(await myPromise);
43+
}
44+
45+
myDisplay();
46+
47+
//
48+
function myFirst() {
49+
console.log("Kirttinath");
50+
}
51+
52+
function mySecond() {
53+
console.log("Goodbye");
54+
}
55+
mySecond(); //this will execute first
56+
myFirst(); //second this is execute

closure.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
function f1() {
2-
var a = 100;
3-
/*return function f2(){
2+
var a = 100;
3+
/*return function f2(){
44
console.log(a);
55
66
}*/
7-
function f2() {
8-
console.log(a);
9-
}
10-
11-
return f2;
7+
function f2() {
8+
console.log(a);
9+
}
1210

11+
return f2;
1312
}
14-
var c = f1();
15-
console.log(c);
16-
c();
13+
f1()();
14+
15+
//? Closures :
16+
//? Closure is an important JavaScript pattern to give private access to a variable
1717

18-
//Closures :
19-
//Closure is an important JavaScript pattern to give private access to a variable
2018
function closures(val) {
21-
return function (name) {
22-
console.log(val + " " + name);
23-
}
19+
return function (name) {
20+
console.log(val + " " + name);
21+
};
2422
}
2523
const greet = closures("Hello");
2624
greet("Kirtti");
2725

28-
//In a more real-world scenario, you could envision an initial function apiConnect(apiKey) that returns some methods that would use the API key. In this case, the apiKey would just need to be provided once and never again.
26+
//* In a more real-world scenario, you could envision an initial function apiConnect(apiKey) that returns some methods that would use the API key. In this case, the apiKey would just need to be provided once and never again.
2927

3028
function apiConnect(apiKey) {
31-
function get(route) {
32-
return fetch(`${route}?key=${apiKey}`);
33-
}
34-
35-
36-
37-
function post(route, params) {
38-
return fetch(route, { method: 'POST', body: JSON.stringify(params), headers: { 'Authorization': `Bearer ${apiKey}` } }) }
39-
40-
41-
return { get, post }
29+
function get(route) {
30+
return fetch(`${route}?key=${apiKey}`);
31+
}
32+
33+
function post(route, params) {
34+
return fetch(route, {
35+
method: "POST",
36+
body: JSON.stringify(params),
37+
headers: { Authorization: `Bearer ${apiKey}` },
38+
});
39+
}
40+
41+
return { get, post };
4242
}
4343

44-
const api = apiConnect('my-secret-key');
44+
const api = apiConnect("my-secret-key");

0 commit comments

Comments
 (0)