Skip to content

Commit 433c9dc

Browse files
KatabathulaKatabathula
Katabathula
authored and
Katabathula
committed
general javascript learnings
1 parent cc85a49 commit 433c9dc

12 files changed

+179
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

arguments-length-changes.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function argumentTest(a, b, c) {
2+
console.log(arguments.length, ' :: ', arguments);
3+
}
4+
argumentTest();
5+
argumentTest('a');
6+
argumentTest('a', 'b');
7+
argumentTest('a', 'b', 'c');
8+
argumentTest('a', 'b', 'c', 'd');

const-scenarios.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const stringConstant = "Constant";
2+
const arrayConstant = ["s1","s2","s3","S4"];
3+
const objectConstant = {
4+
"key1":"val1",
5+
"key2":"val2",
6+
"key3":"val3",
7+
"key4":"val4"
8+
};
9+
const integerConstant = 2;
10+
console.log("*********************Initial values**********************");
11+
console.log("integerConstant : ",integerConstant);
12+
console.log("stringConstant : ",stringConstant);
13+
console.log("arrayConstant : ",arrayConstant);
14+
console.log("objectConstant : ",objectConstant);
15+
// stringConstant = "testChange"; // throws error as assignment is not permitted
16+
// integerConstant = 3; // throws error as assignment is not permitted
17+
// arrayConstant = [1,2,3,4]; // throws error as assignment is not permitted
18+
// objectConstant = {"key1":"val2"}; // throws error as assignment is not permitted
19+
// arrayConstant[0]="string1"; // Allowed since we are just changing a single value of arrayConstant
20+
arrayConstant[0]=objectConstant; // Allowed since we are just changing a single value of arrayConstant
21+
objectConstant["key1"] =stringConstant; // Allowed since we are just changing a single value of objectConstant
22+
23+
console.log("\n\n*********************After changes***********************");
24+
console.log("integerConstant : ",integerConstant);
25+
console.log("stringConstant : ",stringConstant);
26+
console.log("arrayConstant : ",arrayConstant);
27+
console.log("objectConstant : ",objectConstant);

ds/arguments.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function arguments() {
2+
3+
}
4+
5+
function magicCalc(init) {
6+
this.add = function (argument) {
7+
if (!init)
8+
this.init = 0;
9+
typeof (argument) === 'number'
10+
? init += argument
11+
: ((typeof (argument) === 'array')
12+
? argument.reduce(function (sum, value) { }, this.init)
13+
: this.init
14+
);
15+
return this;
16+
}
17+
}
18+
19+
console.log(new magicCalc(0).add([1]));

ds/chaining.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let Calculator = function (init) {
2+
this.add = function (x) {
3+
init += x;
4+
return this;
5+
}
6+
this.multiply = function (x) {
7+
init *= x;
8+
return this;
9+
}
10+
this.print = function () {
11+
console.log(init);
12+
}
13+
}
14+
new Calculator(0).add(1).add(2).print();

for-each-vs-map.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@ a = a.map(element => {
55
console.log(element);
66
element = element * 2;
77
return element;
8+
// return is required because map creates a new array pushing the values returned
89
});
910

1011
b.forEach((element) => {
1112
element *= 2;
1213
// return element;
14+
// no need to return as the elements get updated automatically
1315
});
1416
console.log('a : ', a);
1517
console.log('b : ', b);
16-
17-
// for (let test of a) {
18-
// console.log(' of : ',test);
19-
// }
20-
// for (let test in a) {
21-
// console.log(' in : ', test);
22-
// }
23-
24-
// a = { test: 1, test2: 2, test3: 3, test4: 4 };
25-
// for(let test of a) {
26-
// console.log(' of : ',test);
27-
// }

let-vs-var.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let a = function() {
1+
let letFunc = function() {
22
let a = 7, b =[];
33
for(var i =0;i<a;i++) {
44
b.push(()=>{
@@ -10,7 +10,7 @@ let a = function() {
1010
});
1111
}
1212
};
13-
let a2 = function () {
13+
let varFunc = function () {
1414
let a = 7, b = [];
1515
for (var i = 0; i < a; i++) {
1616
b.push(() => {
@@ -24,8 +24,8 @@ let a2 = function () {
2424
}
2525
};
2626

27-
let letFunc = a2,varFunc = a;
27+
// let letFunc = a2,varFunc = a;
2828
console.log('var one');
2929
letFunc.print();
3030
console.log('let one');
31-
varFunc.print();
31+
varFunc.print();

object-creation.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let obj1 = new Object();
2+
console.log(obj1);
3+
let obj2 = {};
4+
console.log(obj2);
5+
6+
console.log(obj1 == obj2);
7+
console.log(obj1 === obj2);

object-ref-vs-clone.js

Whitespace-only changes.

strings-reference.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let a = "goku", b = "goku";
2+
console.log('a : ', a);
3+
console.log('b : ', b);
4+
b = "vegeta";
5+
console.log('after b = "vegita"');
6+
console.log('a : ', a);
7+
console.log('b : ', b);
8+
b = a;
9+
console.log('after b = a');
10+
console.log('a : ', a);
11+
console.log('b : ', b);
12+
a = "vegito";
13+
console.log('after a = "vegito"');
14+
console.log('a : ', a);
15+
console.log('b : ', b);
16+
17+
b="buu";
18+
console.log('after setting b to max')
19+
console.log('a : ', a);
20+
console.log('b : ', b);

test-string-pool.js

-6
This file was deleted.

types/basic-types.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
let str = '1', num = 2;
2+
let boolTrue = true, boolFalse = false;
3+
console.log('**** String and Number ****');
4+
console.log('----------------------------');
5+
console.log('\'1\' + 2 :: ', str + num);
6+
console.log('\'1\' - 2 :: ', str - num);
7+
console.log('\'1\' * 2 :: ', str * num);
8+
console.log('\'1\' / 2 :: ', str / num);
9+
console.log('\'1\' % 2 :: ', str % num);
10+
11+
console.log('\n**** Number and String ****');
12+
console.log('-----------------------------');
13+
console.log('2 + \'1\' :: ', num + str);
14+
console.log('2 - \'1\' :: ', num - str);
15+
console.log('2 * \'1\' :: ', num * str);
16+
console.log('2 / \'1\' :: ', num / str);
17+
console.log('2 % \'1\' :: ', num % str);
18+
19+
console.log('\n*** Number and True ***');
20+
console.log('----------------------------');
21+
console.log('2 + true :: ', num + boolTrue);
22+
console.log('2 + true :: ', num + boolTrue);
23+
console.log('2 + true :: ', num + boolTrue);
24+
console.log('2 + true :: ', num + boolTrue);
25+
console.log('2 + true :: ', num + boolTrue);
26+
27+
console.log('\n*** Number and False ***');
28+
console.log('----------------------------');
29+
console.log('2 + false :: ', num + boolFalse);
30+
console.log('2 - false :: ', num - boolFalse);
31+
console.log('2 * false :: ', num * boolFalse);
32+
console.log('2 / false :: ', num / boolFalse);
33+
console.log('2 % false :: ', num % boolFalse);
34+
35+
console.log('\n*** String and boolean ***');
36+
console.log('----------------------------');
37+
console.log('**** true and String ****');
38+
console.log('-------------------------');
39+
console.log('true + \'1\'', boolTrue + str);
40+
console.log('true - \'1\'', boolTrue - str);
41+
console.log('true * \'1\'', boolTrue * str);
42+
console.log('true / \'1\'', boolTrue / str);
43+
console.log('true % \'1\'', boolTrue % str);
44+
45+
console.log('**** false and String ****');
46+
console.log('-------------------------');
47+
console.log('false + \'1\'', boolFalse + str);
48+
console.log('false - \'1\'', boolFalse - str);
49+
console.log('false * \'1\'', boolFalse * str);
50+
console.log('false / \'1\'', boolFalse / str);
51+
console.log('false % \'1\'', boolFalse % str);
52+
53+
console.log('\n*** Boolean Operations ***');
54+
console.log('============================');
55+
console.log('\n**** true and false ****');
56+
console.log('--------------------------');
57+
console.log('true + false :: ', boolTrue + boolFalse);
58+
console.log('true - false :: ', boolTrue - boolFalse);
59+
console.log('true * false :: ', boolTrue * boolFalse);
60+
console.log('true / false :: ', boolTrue / boolFalse);
61+
console.log('true + false :: ', boolTrue + boolFalse);
62+
63+
console.log('\n**** All true Operations ****')
64+
console.log('-------------------------------');
65+
console.log('true + true :: ', boolTrue + boolTrue);
66+
console.log('true - true :: ', boolTrue - boolTrue);
67+
console.log('true * true :: ', boolTrue * boolTrue);
68+
console.log('true / true :: ', boolTrue / boolTrue);
69+
console.log('true % true :: ', boolTrue % boolTrue);
70+
71+
console.log('\n**** All false Operations ****')
72+
console.log('--------------------------------');
73+
console.log('false + false :: ', boolFalse + boolFalse);
74+
console.log('false - false :: ', boolFalse - boolFalse);
75+
console.log('false * false :: ', boolFalse * boolFalse);
76+
console.log('false / false :: ', boolFalse / boolFalse);
77+
console.log('false % false :: ', boolFalse % boolFalse);

0 commit comments

Comments
 (0)