Skip to content

Commit b24a9eb

Browse files
committed
Changed function definitions in iife.js, life.js, and pureimpurefunc.js.
1 parent f545350 commit b24a9eb

File tree

4 files changed

+116
-55
lines changed

4 files changed

+116
-55
lines changed

String_Methods.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// 1. length
2+
const str = "Hello, World!";
3+
console.log(str.length); // Output: 13
4+
5+
// 2. toUpperCase
6+
console.log(str.toUpperCase()); // Output: HELLO, WORLD!
7+
8+
// 3. toLowerCase
9+
console.log(str.toLowerCase()); // Output: hello, world!
10+
11+
// 4. concat
12+
const firstName = "John";
13+
const lastName = "Doe";
14+
console.log(firstName.concat(" ", lastName)); // Output: John Doe
15+
16+
// 5. indexOf
17+
console.log(str.indexOf("World")); // Output: 7
18+
19+
// 6. lastIndexOf
20+
console.log(str.lastIndexOf("o")); // Output: 8
21+
22+
// 7. charAt
23+
console.log(str.charAt(0)); // Output: H
24+
25+
// 8. charCodeAt
26+
console.log(str.charCodeAt(0)); // Output: 72
27+
28+
// 9. substring
29+
console.log(str.substring(7, 12)); // Output: World
30+
31+
// 10. slice
32+
console.log(str.slice(7, 12)); // Output: World
33+
34+
// 11. replace
35+
console.log(str.replace("World", "Universe")); // Output: Hello, Universe!
36+
37+
// 12. trim
38+
const paddedStr = " Hello ";
39+
console.log(paddedStr.trim()); // Output: Hello
40+
41+
// 13. split
42+
const words = str.split(", ");
43+
console.log(words); // Output: ['Hello', 'World!']
44+
45+
// 14. startsWith
46+
console.log(str.startsWith("Hello")); // Output: true
47+
48+
// 15. endsWith
49+
console.log(str.endsWith("World!")); // Output: true
50+
51+
// 16. includes
52+
console.log(str.includes("lo")); // Output: true
53+
54+
// 17. repeat
55+
console.log("La ".repeat(3)); // Output: La La La
56+
57+
// 18. match
58+
const regex = /[A-Z]/g;
59+
console.log(str.match(regex)); // Output: ['H', 'W']
60+
61+
// 19. padStart
62+
console.log(str.padStart(20, "*")); // Output: ***Hello, World!
63+
64+
// 20. padEnd
65+
console.log(str.padEnd(20, "*")); // Output: Hello, World!***
66+
67+
// 21. toLocaleUpperCase
68+
console.log(str.toLocaleUpperCase("tr-TR")); // Output: HELLO, WORLD!
69+
70+
// 22. toLocaleLowerCase
71+
console.log(str.toLocaleLowerCase("tr-TR")); // Output: hello, world!

iife.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
//IIFE : Immidiately Invoked Function Expression
22
//It is a function in JS which immidiately invoked and executed as soon as defined
3-
let paintColor = 'red'
4-
const paint = (() => {//This is an IIFE that defines an object with two methods (changeColorToBlue and
5-
//changeColorToGreen). The IIFE is executed immediately, creating and returning
6-
//the paint object.
7-
return {
8-
changeColorToBlue: () => {
9-
paintcolor: 'Blue';
10-
return paintColor;
11-
},
12-
changeColorToGreen: () => {
13-
paintColor: 'Green';
14-
return paintColor;
15-
}
16-
}
3+
let paintColor = "red";
4+
const paint = (() => {
5+
//This is an IIFE that defines an object with two methods (changeColorToBlue and
6+
//changeColorToGreen). The IIFE is executed immediately, creating and returning
7+
//the paint object.
8+
return {
9+
changeColorToBlue: () => {
10+
paintColor: "Blue";
11+
return paintColor;
12+
},
13+
changeColorToGreen: () => {
14+
paintColor: "Green";
15+
return paintColor;
16+
},
17+
};
1718
})();
18-
console.log(
19-
paint.changeColorToBlue()
20-
);
21-
19+
console.log(paint.changeColorToBlue());

life.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
//Life in Javascript :
1+
//IIFE in Javascript :
22
// Immidiately invoked function expression
33

4-
5-
var ans = (function()
6-
{
7-
var prvtval =12;
8-
return {
9-
getter: function()
10-
{
11-
console.log(prvtval);
12-
},
13-
setter: function(value)
14-
{
15-
prvtval = val;
16-
}
17-
}
18-
19-
})()
4+
var ans = (function () {
5+
var prvtval = 12;
6+
return {
7+
getter: function () {
8+
console.log(prvtval);
9+
},
10+
setter: function (value) {
11+
prvtval = val;
12+
},
13+
};
14+
})();

pureimpurefunc.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
//Pure and Impure function
1+
//Pure and Impure function
22
//pure :
3-
//Pure function is any function ehich has 2 features
3+
//Pure function is any function which has 2 features
44
//1. It should always return same output for same input
55
//2.It will never change/update the value of the global variable
66
//ex :
7-
function prod(a,b)
8-
{
9-
return a*b; // This is a pure function
7+
function prod(a, b) {
8+
return a * b; // This is a pure function
109
}
11-
var ans1 = prod(1,2);
12-
var ans2 = prod(1,2);
13-
console.log(ans1,ans2);
10+
var ans1 = prod(1, 2);
11+
var ans2 = prod(1, 2);
12+
console.log(ans1, ans2);
1413

1514
// If any of two conditions are not maintain then impure function created
1615
// Impure function :
1716
//ex :
18-
var x=12;
19-
function prod1(a,b)
20-
{
21-
var x=15;
22-
return a*b; // although it gives same output, This is not a pure function
17+
var x = 12;
18+
function prod1(a, b) {
19+
var x = 15;
20+
return a * b; // although it gives same output, This is not a pure function
2321
}
24-
var ans1 = prod1(1,2);
25-
var ans2 = prod1(1,2);
26-
console.log(ans1,ans2);
22+
var ans1 = prod1(1, 2);
23+
var ans2 = prod1(1, 2);
24+
console.log(ans1, ans2);
2725
//or
28-
function out(a)
29-
{
30-
return Math.random()*a;
26+
function out(a) {
27+
return Math.random() * a;
3128
}
3229
var res1 = out(2);
3330
var res2 = out(2);
34-
console.log(res1,res2);
31+
console.log(res1, res2);

0 commit comments

Comments
 (0)