ganesh-is-prime 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/JS_Practice/callbacks.js +45 -0
- package/JS_Practice/closure.js +41 -0
- package/JS_Practice/functions.js +105 -0
- package/JS_Practice/promises.js +65 -0
- package/JS_Practice/scope.js +36 -0
- package/JS_Practice/set_interval.js +21 -0
- package/JS_Practice/var_let_const +48 -0
- package/deek_package.js +9 -0
- package/main.js +2 -0
- package/my-package/package.json +12 -0
- package/npm_package/README.md +0 -0
- package/npm_package/index.js +9 -0
- package/npm_package/index.test.js +7 -0
- package/npm_package/package.json +19 -0
- package/package.json +7 -0
- package/server.js +11 -0
- package/test.js +7 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// setInterval(()=>{
|
|
2
|
+
// console.log("hi");
|
|
3
|
+
// },1000);
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
// console.log("Start");
|
|
7
|
+
|
|
8
|
+
// setTimeout(() => {
|
|
9
|
+
// console.log("Timer finished");
|
|
10
|
+
// }, 1000);
|
|
11
|
+
|
|
12
|
+
// console.log("End");
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
function processUser(userId, callback) {
|
|
17
|
+
console.log("Fetching user...");
|
|
18
|
+
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
if (!userId) {
|
|
21
|
+
callback(new Error("User ID is required"), null);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const user = {
|
|
26
|
+
id: userId,
|
|
27
|
+
name: "Ganesh"
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
callback(null, user);
|
|
31
|
+
}, 1000);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log("Start");
|
|
35
|
+
|
|
36
|
+
processUser(101, (error, user) => {
|
|
37
|
+
if (error) {
|
|
38
|
+
console.error("Error:", error.message);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log("User received:", user);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log("End");
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
function outer() {
|
|
2
|
+
const message = "Hello";
|
|
3
|
+
|
|
4
|
+
function inner() {
|
|
5
|
+
console.log(message);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return inner;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const fn = outer();
|
|
12
|
+
|
|
13
|
+
fn(); // Hello
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
function outer() {
|
|
18
|
+
const x = 10;
|
|
19
|
+
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
console.log(x);
|
|
22
|
+
}, 1000);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
outer();
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
function createCounter() {
|
|
29
|
+
let count = 0;
|
|
30
|
+
|
|
31
|
+
return function () {
|
|
32
|
+
count++;
|
|
33
|
+
return count;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const counter = createCounter();
|
|
38
|
+
|
|
39
|
+
console.log(counter()); // 1
|
|
40
|
+
console.log(counter()); // 2
|
|
41
|
+
console.log(counter()); // 3
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// console.log(calculateTotal(100, 20)); // 120
|
|
2
|
+
|
|
3
|
+
// function calculateTotal(price, tax) {
|
|
4
|
+
|
|
5
|
+
// const discount = calculateDiscount(price);
|
|
6
|
+
|
|
7
|
+
// function calculateDiscount(amount) {
|
|
8
|
+
// return amount * 0.00;
|
|
9
|
+
// }
|
|
10
|
+
|
|
11
|
+
// function applyTax(amount) {
|
|
12
|
+
// return amount + tax;
|
|
13
|
+
// }
|
|
14
|
+
|
|
15
|
+
// const finalAmount = applyTax(price - discount);
|
|
16
|
+
|
|
17
|
+
// return finalAmount;
|
|
18
|
+
// }
|
|
19
|
+
|
|
20
|
+
// const calculator = calculateTotal;
|
|
21
|
+
|
|
22
|
+
// console.log(calculator(200, 30)); // 230
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// greet();
|
|
29
|
+
|
|
30
|
+
// const greet = function () {
|
|
31
|
+
// console.log("Hello");
|
|
32
|
+
// };
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// Function expression with var
|
|
36
|
+
console.log(typeof multiply); // undefined
|
|
37
|
+
// multiply(); // TypeError
|
|
38
|
+
|
|
39
|
+
var multiply = function (a, b) {
|
|
40
|
+
return a * b;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
console.log(multiply(5, 4)); // 20
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// Named function expression
|
|
47
|
+
const factorial = function fact(n) {
|
|
48
|
+
if (n <= 1) {
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return n * fact(n - 1);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
console.log(factorial(5)); // 120
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
//arrow functions
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
const user = {
|
|
63
|
+
name: "Ganesh",
|
|
64
|
+
age: 21,
|
|
65
|
+
|
|
66
|
+
// Normal method: gets its own dynamic `this`
|
|
67
|
+
greet() {
|
|
68
|
+
console.log(`Hello, I am ${this.name}`);
|
|
69
|
+
|
|
70
|
+
// Arrow function inherits `this` from greet()
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
console.log(`I am still ${this.name}`);
|
|
73
|
+
}, 100);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
user.greet();
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
//Function parameters , arguments , return
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
const calculateBill = (price, tax = 0.18, ...discounts) => {
|
|
84
|
+
let finalPrice = price;
|
|
85
|
+
|
|
86
|
+
// `arguments` is not used here because this is an arrow function.
|
|
87
|
+
// `discounts` is the rest parameter and contains extra arguments.
|
|
88
|
+
|
|
89
|
+
for (const discount of discounts) {
|
|
90
|
+
finalPrice -= discount;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return finalPrice + finalPrice * tax;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const result = calculateBill(1000, undefined, 50, 100);
|
|
97
|
+
|
|
98
|
+
console.log(result);
|
|
99
|
+
|
|
100
|
+
//IIFE
|
|
101
|
+
(function () {
|
|
102
|
+
const bill = calculateBill(1000, 0.18, 50, 100);
|
|
103
|
+
|
|
104
|
+
console.log("Initial bill:", bill);
|
|
105
|
+
})();
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// const promise = new Promise((resolve,reject)=>{
|
|
2
|
+
// resolve(10);
|
|
3
|
+
// }).then((resolve)=>{
|
|
4
|
+
// console.log(resolve);
|
|
5
|
+
// })
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
// function getUser() {
|
|
9
|
+
// return new Promise((resolve) => {
|
|
10
|
+
// setTimeout(() => {
|
|
11
|
+
// resolve({
|
|
12
|
+
// id: 101,
|
|
13
|
+
// name: "Ganesh"
|
|
14
|
+
// });
|
|
15
|
+
// }, 0);
|
|
16
|
+
// });
|
|
17
|
+
// }
|
|
18
|
+
|
|
19
|
+
// const result = getUser();
|
|
20
|
+
|
|
21
|
+
// console.log(result);
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
function fetchUser(userId) {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
console.log("1. Promise executor started");
|
|
30
|
+
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
if (!userId) {
|
|
33
|
+
reject(new Error("User ID is required"));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const user = {
|
|
38
|
+
id: userId,
|
|
39
|
+
name: "Ganesh"
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
resolve(user);
|
|
43
|
+
}, 1000);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
console.log("2. Calling fetchUser()");
|
|
48
|
+
|
|
49
|
+
fetchUser(101)
|
|
50
|
+
.then(user => {
|
|
51
|
+
console.log("3. User received:", user);
|
|
52
|
+
|
|
53
|
+
return `Welcome ${user.name}`;
|
|
54
|
+
})
|
|
55
|
+
.then(message => {
|
|
56
|
+
console.log("4. Message:", message);
|
|
57
|
+
})
|
|
58
|
+
.catch(error => {
|
|
59
|
+
console.log("5. Error:", error.message);
|
|
60
|
+
})
|
|
61
|
+
.finally(() => {
|
|
62
|
+
console.log("6. Finished");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
console.log("7. JavaScript continues...");
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const company = "PalTech"; // Global scope
|
|
2
|
+
|
|
3
|
+
function employee(name) { // name = function parameter
|
|
4
|
+
|
|
5
|
+
const role = "Developer"; // Function scope
|
|
6
|
+
|
|
7
|
+
if (true) {
|
|
8
|
+
const level = "Junior"; // Block scope
|
|
9
|
+
let salary = 50000; // Block scope
|
|
10
|
+
var city = "Hyderabad"; // Function scope because var
|
|
11
|
+
|
|
12
|
+
console.log(name); // Ganesh
|
|
13
|
+
console.log(role); // Developer
|
|
14
|
+
console.log(level); // Junior
|
|
15
|
+
console.log(salary); // 50000
|
|
16
|
+
console.log(company); // PalTech
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log(city); // Hyderabad
|
|
20
|
+
// console.log(level); // ❌ ReferenceError
|
|
21
|
+
// console.log(salary); // ❌ ReferenceError
|
|
22
|
+
|
|
23
|
+
function introduce() {
|
|
24
|
+
const message = "Hello";
|
|
25
|
+
|
|
26
|
+
console.log(name); // Ganesh
|
|
27
|
+
console.log(role); // Developer
|
|
28
|
+
console.log(company); // PalTech
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
introduce();
|
|
32
|
+
|
|
33
|
+
// console.log(message); // ❌ ReferenceError
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
employee("Ganesh");
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
let count = 0;
|
|
2
|
+
|
|
3
|
+
const id = setInterval(() => {
|
|
4
|
+
count++;
|
|
5
|
+
|
|
6
|
+
console.log(count);
|
|
7
|
+
|
|
8
|
+
if (count === 3) {
|
|
9
|
+
clearInterval(id);
|
|
10
|
+
}
|
|
11
|
+
}, 1000);
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
console.log("Start");
|
|
16
|
+
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
console.log("Timer");
|
|
19
|
+
}, 2000);
|
|
20
|
+
|
|
21
|
+
console.log("End");
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var a = 10;
|
|
2
|
+
let b = 20;
|
|
3
|
+
const c = 30;
|
|
4
|
+
|
|
5
|
+
a = 11; // ✅ reassignment
|
|
6
|
+
b = 21; // ✅ reassignment
|
|
7
|
+
// c = 31; // ❌ TypeError
|
|
8
|
+
|
|
9
|
+
var a = 100; // ✅ redeclaration
|
|
10
|
+
// let b = 200; // ❌ SyntaxError
|
|
11
|
+
// const c = 300; // ❌ SyntaxError
|
|
12
|
+
|
|
13
|
+
if (true) {
|
|
14
|
+
var x = "var";
|
|
15
|
+
let y = "let";
|
|
16
|
+
const z = "const";
|
|
17
|
+
|
|
18
|
+
console.log(x); // var
|
|
19
|
+
console.log(y); // let
|
|
20
|
+
console.log(z); // const
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(x); // "var"
|
|
24
|
+
|
|
25
|
+
// console.log(y); // ❌ ReferenceError
|
|
26
|
+
// console.log(z); // ❌ ReferenceError
|
|
27
|
+
|
|
28
|
+
const user = {
|
|
29
|
+
name: "Ganesh"
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
user.name = "Sai"; // ✅ mutation
|
|
33
|
+
|
|
34
|
+
// user = { name: "Rahul" }; // ❌ reassignment (creates new object)
|
|
35
|
+
|
|
36
|
+
function test() {
|
|
37
|
+
var inside = 10;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// console.log(inside); // ❌ ReferenceError
|
|
41
|
+
|
|
42
|
+
//hoisting
|
|
43
|
+
|
|
44
|
+
console.log(hoistedVar); // undefined
|
|
45
|
+
var hoistedVar = 50;
|
|
46
|
+
|
|
47
|
+
// console.log(hoistedLet); // ❌ ReferenceError
|
|
48
|
+
let hoistedLet = 60;
|
package/deek_package.js
ADDED
package/main.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sai_ganesh_29/is-prime",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "is your number a prime?",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"prime"
|
|
7
|
+
],
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://gitlab.pal.tech/ganesh.chintalapalli/npm_package.git"
|
|
11
|
+
},
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"author": "sai ganesh",
|
|
14
|
+
"type": "commonjs",
|
|
15
|
+
"main": "index.js",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node index.test.js"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/package.json
ADDED
package/server.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const { createServer } = require('node:http');
|
|
2
|
+
const hostname = '127.0.0.1';
|
|
3
|
+
const port = 6969;
|
|
4
|
+
const server = createServer((req, res) => {
|
|
5
|
+
res.statusCode = 200;
|
|
6
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
7
|
+
res.end('Hello World');
|
|
8
|
+
});
|
|
9
|
+
server.listen(port, hostname, () => {
|
|
10
|
+
console.log(`Server running at http://${hostname}:${port}/`);
|
|
11
|
+
});
|