Skip to content

Commit 7da33b5

Browse files
author
Imam Hossain Santho
committed
Js concepts
1 parent e221ae7 commit 7da33b5

14 files changed

+344
-0
lines changed

1-json.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require('fs')
2+
3+
const book = {
4+
name: 'Imam Hossain',
5+
year: 2014
6+
}
7+
// const bookJSON = JSON.stringify(book);
8+
// console.log(bookJSON);
9+
// fs.writeFileSync('./1-json.json', bookJSON);
10+
// const bookData = JSON.parse(bookJSON);
11+
// console.log(bookData);
12+
let dataJSON = fs.readFileSync('1-json.json').toString()
13+
const user = JSON.parse(dataJSON);
14+
user.year = 1996;
15+
16+
const userJSON = JSON.stringify(user);
17+
console.log(userJSON);
18+
fs.writeFileSync('./1-json.json', userJSON);

2.arrow functions.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const button = document.createElement("button")
2+
button.textContent = "Click me"
3+
document.body.append(button)
4+
5+
class Display {
6+
constructor() {
7+
this.buttonText = "New text"
8+
9+
button.addEventListener("click", (event) => {
10+
event.target.textContent = this.buttonText //this context refers to outher this.buttonText
11+
})
12+
}
13+
}
14+
15+
new Display() //New Text Display
16+
//without arrow function, it will be "click me"

3.iife.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let taskArr = (() => {
2+
let arr = []
3+
for (let i = 1; i <= 5; i++) {
4+
arr.push({
5+
name: "Task-1" + i,
6+
completed: i % 2 === 0,
7+
})
8+
}
9+
return arr
10+
})()
11+
12+
console.log(taskArr)

4-callbacks.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* const geocode = (address, callback) => {
2+
return setTimeout(() => {
3+
const data = {
4+
latitude: 0,
5+
longitude: 0
6+
}
7+
callback(address, data)
8+
}, 2000)
9+
}
10+
function getData(address, data){
11+
console.log(address, ':\n' , data);
12+
}
13+
geocode('Philadelpia', getData) */
14+
15+
/*******calllback */
16+
/* add(1, 2, sum)
17+
function add(a, b, sum) {
18+
sum(a+b)
19+
}
20+
function sum(result) {
21+
console.log(result)
22+
} */
23+
/*****closure */
24+
/* function mul(a, b) {
25+
return function mulCallback(c) {
26+
console.log(a * b * c)
27+
}
28+
}
29+
let x = mul(1, 2)
30+
31+
32+
console.log(x)
33+
x(5)
34+
*/
35+
36+
/*****Callback again */
37+
38+
const doRocketLaunch = (weather, callbackResult) => {
39+
setTimeout(function checkingWeather() {
40+
if (weather == 'Good') callbackResult(undefined, 'Mission Success!')
41+
42+
else callbackResult('Abort Mission', undefined)
43+
}, 2000)
44+
45+
console.log('Checking weather....')
46+
}
47+
const callbackResult = (error, result) => {
48+
if (error) {
49+
return console.log(error);
50+
}
51+
console.log(result);
52+
}
53+
54+
doRocketLaunch('Good', callbackResult)
55+
//doRocketLaunch('Bad', callbackResult)

5-es6-objects.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
let name = "Imam"
2+
let myAge= 25
3+
4+
let user = {
5+
name,
6+
age: myAge,
7+
location: 'Bangladesh'
8+
}
9+
// console.log(user) //{ name: 'Imam', age: 25, location: 'Bangladesh' }
10+
11+
12+
13+
//Destructuring
14+
15+
let product = {
16+
label: "You don't know JS",
17+
price: 10 + '$' ,
18+
stock: 201,
19+
salePrice: undefined,
20+
// rating: 4.2
21+
}
22+
// let label = product.label
23+
// let price = product.price
24+
25+
let {label: productLabel, price, rating = 5} = product
26+
//console.log(productLabel, price, rating )
27+
28+
let transaction = (type, {label, price, stock = 0} = {}) => {
29+
console.log(type, label, price, stock)
30+
}
31+
transaction('order', product)
32+
33+
34+
/****Objeect property */
35+
36+
const userName = [
37+
{
38+
name: 'Imam'
39+
},
40+
{
41+
name: 'Shuvro'
42+
},
43+
{
44+
name: 'Santho'
45+
}
46+
]
47+
console.log({ name })

6-raw-http.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const https = require('https')
2+
3+
let lat = 23.7104, lon = 90.4074; //Dhaka, BD
4+
5+
const url = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&units=metric&exclude=daily&appid=a914e84824e3adbafcdfc73e8b8889e4`
6+
7+
const request = https.request(url, (response) => {
8+
let data = ''
9+
10+
response.on('data', (chunk) => {
11+
console.log(chunk)
12+
console.log('*********************************')
13+
data = data + chunk.toString()
14+
})
15+
response.on('end', () => {
16+
const body = JSON.parse(data)
17+
console.log(body)
18+
})
19+
})
20+
request.on('error', (error) => {
21+
//console.log('An error!', error)
22+
})
23+
24+
request.end()

7-default-params.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let greater = (name = '', age = 0) => {
2+
console.log('Hello ' + name, '\t You are ' + age + ' years old')
3+
}
4+
5+
greater('Imam')
6+
7+
greater()
8+
greater( '', 24)
9+
10+
greater('Imam ', 24 , 'extra')

8-promise-chaining.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const add = (a, b) => {
2+
return new Promise((resolve, reject) => {
3+
setTimeout(() => {
4+
resolve(a + b)
5+
}, 1000)
6+
})
7+
}
8+
9+
const sum = add(1, 2)
10+
.then((sum) => {
11+
console.log(sum)
12+
return add(sum, 5)
13+
})
14+
.then((sum) => console.log(sum))
15+
.catch((err) => console.log(err))

8-promises.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const doRocketLaunch = (weather) => {
2+
return new Promise((resolve, reject) => {
3+
setTimeout(function gettingWeatherData() {
4+
if (weather == "Good") resolve("Mission Success")
5+
else reject("Abort Mission")
6+
}, 2000)
7+
})
8+
}
9+
10+
doRocketLaunch("Good")
11+
.then((result) => console.log(result))
12+
.catch((error) => console.log(error))

9.async-await.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const add = (a, b) => {
2+
return new Promise((resolve, reject) => {
3+
setTimeout(() => {
4+
if (a < 0 || b < 0) reject(new Error('Negative number not supported'))
5+
resolve(a + b)
6+
}, 1000)
7+
})
8+
}
9+
10+
const doWork = async () => {
11+
const sum = await add(1, 2)
12+
const sum2 = await add(sum, 5)
13+
const sum3 = await add(sum2, 10)
14+
15+
return sum3
16+
}
17+
doWork()
18+
.then((sum) => console.log(sum))
19+
.catch((e) => console.log(e))
20+
21+
async function some(a) {
22+
if (a < 10) throw new Error('Less then 10')
23+
24+
return a + 10
25+
}
26+
27+
async function another() {
28+
try {
29+
const a = await some(10)
30+
31+
console.log(a)
32+
}
33+
catch {
34+
console.log(error)
35+
}
36+
}
37+
38+
another()

0 commit comments

Comments
 (0)