Skip to content

Commit fccdf7f

Browse files
committed
Completes content
1 parent 0264746 commit fccdf7f

9 files changed

Lines changed: 308 additions & 5 deletions

File tree

03_basics/03_arrow.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ const user= {
2727
//We are not able to get the context within a function
2828
//like using this within didn't return an object, instead, we get global value, performace evaluations etc
2929

30-
// const chai(){
31-
// let username="Prakhar"
32-
// console.log(this.username);
33-
// }
34-
// chai()
30+
const chai=function(){
31+
let username="Prakhar"
32+
console.log(this.username);
33+
}
34+
chai()
3535
//Output: Here the output we get is actually undefined
3636

3737

05_iterations/eight.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const myNums= [1,2,3]
2+
3+
// const myTotal= myNums.reduce(function(acc, currval) {
4+
// console.log(`acc: ${acc} and currval: ${currval}`);
5+
// return acc+currval},0)
6+
7+
const myTotal = myNums.reduce((acc, curr)=>acc+curr,0)
8+
9+
// console.log(myTotal);
10+
11+
const shoppingCart=[
12+
{
13+
itemName: "JS Course",
14+
price: 2999
15+
},
16+
{
17+
itemName: "Python Course",
18+
price: 2999
19+
},
20+
{
21+
itemName: "Java Course",
22+
price: 4999
23+
},
24+
{
25+
itemName: "Data Science Course",
26+
price: 7999
27+
},
28+
]
29+
const priceToPay=shoppingCart.reduce((acc,item)=>acc+item.price,0)
30+
console.log(priceToPay);

05_iterations/five.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const coding = ["js", "ruby", "java", "python", "cpp"]
2+
3+
//foreach
4+
//In for each loop we are creating a callback. A callback doesn't have a name
5+
//As a parameter, we take val(can take anything), for iterating through values in coding
6+
// coding.forEach( function (val) {
7+
// console.log(val);
8+
// })
9+
10+
//As a callback, we can use arrpw functions as well
11+
// coding.forEach( (item) => {console.log(item);
12+
// } )
13+
14+
function printMe(item){
15+
console.log(item);
16+
}
17+
// coding.forEach(printMe)
18+
//Note: Here, we giving reference of the function printMe, we are not executing it
19+
20+
// coding.forEach((item, index, arr)=>{
21+
// console.log(item, index, arr);
22+
// })
23+
24+
const myCoding=[
25+
{
26+
languageName:"javascript",
27+
languageFileName: "js"
28+
},
29+
{
30+
languageName:"java",
31+
languageFileName: "java"
32+
},
33+
{
34+
languageName:"python",
35+
languageFileName: "py"
36+
},
37+
]
38+
39+
myCoding.forEach((item)=>{
40+
console.log(item.languageName);
41+
})

05_iterations/four.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Iterating through the objects
2+
const myObject ={
3+
js: 'javascript',
4+
cpp: 'C++',
5+
rb: 'ruby',
6+
swift: 'swift by apple'
7+
}
8+
//forin loop
9+
//Accessing the keys
10+
for (const key in myObject) {
11+
//console.log(key);
12+
}
13+
//Accessing the values
14+
for (const key in myObject) {
15+
//console.log(myObject[key]);
16+
}
17+
18+
for(const key in myObject){
19+
//console.log(`${key} shortcut is for ${myObject[key]}`);
20+
}
21+
22+
//Can for in be used in arrays as well?
23+
24+
const prog=['js','java','go','cpp','py']
25+
for (const key in prog) {
26+
//console.log(key);
27+
}
28+
//This returns index positions of the elements in array
29+
for (const key in prog) {
30+
//console.log(prog[key]);
31+
}
32+
33+
//we can't iterate through maps
34+
const map= new Map()
35+
map.set('IN', "India")
36+
map.set('USA', "United States Of America")
37+
map.set('JP', "Japan")
38+
map.set('Fr', "France")
39+
map.set('IN', "India")
40+
41+
for (const key in map) {
42+
console.log(key);
43+
}
44+
//There is no output for this
45+

05_iterations/one.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// for
2+
3+
for (let i = 0; i <= 10; i++) {
4+
const element = i;
5+
if(element==5){
6+
//console.log("5 is the best number");
7+
}
8+
//console.log(element);
9+
}
10+
11+
//Element is in loop, is not accessible outside due to scope
12+
13+
for (let i = 1; i <=10; i++) {
14+
//console.log(`Outer Loop value: ${i}`);
15+
for (let j = 1; j <=10; j++) {
16+
//console.log(`Inner Loop value ${j} and outer loop ${i}`);
17+
//console.log(i+' * '+j+' = '+i*j);
18+
19+
}
20+
}
21+
22+
let myArray= ["flash", "batman", "superman"]
23+
console.log(myArray.length);
24+
25+
for (let index = 0; index < myArray.length; index++) {
26+
const element = myArray[index];
27+
console.log(element);
28+
}
29+
30+
//In javascript, if we do action for out of bound indexes, we get undefined in its place
31+
// if condition of increasing the index is not there then, we get stuck in an infinite loop
32+
33+
// break and continue
34+
35+
// for (let index = 1; index <= 20; index++) {
36+
// if(index==5){
37+
// console.log("Detected 5");
38+
// break
39+
// }
40+
// console.log(`Value of i is ${index}`);
41+
// }
42+
43+
for (let index = 1; index <= 20; index++) {
44+
if(index==5){
45+
console.log("Detected 5");
46+
continue
47+
}
48+
console.log(`Value of i is ${index}`);
49+
}

05_iterations/seven.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const myNumbers= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2+
3+
// const newNums= myNumbers.map( (num) => num+ 10)
4+
5+
//Chaining
6+
//It means that we are able to apply multiple methods on top of one another
7+
const newNums= myNumbers.map((nums) => nums* 10)
8+
.map((num)=>num*10)
9+
.map((num)=>num+1)
10+
.filter((num)=> num>=40)
11+
12+
console.log(newNums);

05_iterations/six.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const coding = ["js", "ruby", "java", "python", "cpp"]
2+
3+
// const values= coding.forEach((item)=>{
4+
// // console.log(item);
5+
// return item
6+
// })
7+
8+
// console.log(values);
9+
//forEach doesn't return any values
10+
11+
const myNums= [1,2,3,4,5,6,7,8,9,10]
12+
13+
// const newNums= myNums.filter((num) => num>4)
14+
// console.log(newNums);
15+
16+
// const newNums= myNums.filter((num) => {return num>4})
17+
// console.log(newNums);
18+
//If we use curly braces, then without return keyword nothing is returned
19+
20+
const newNums = []
21+
22+
myNums.forEach((num) => {
23+
if(num>4){
24+
newNums.push(num)
25+
}
26+
})
27+
console.log(newNums);
28+
29+
const books = [
30+
{ title: 'Book One', genre: 'Fiction', publish: 1981, edition: 2004 },
31+
{ title: 'Book Two', genre: 'Non-Fiction', publish: 1992, edition: 2008 },
32+
{ title: 'Book Three', genre: 'History', publish: 1999, edition: 2007 },
33+
{ title: 'Book Four', genre: 'Non-Fiction', publish: 1989, edition: 2010 },
34+
{ title: 'Book Five', genre: 'Science', publish: 2009, edition: 2014 },
35+
{ title: 'Book Six', genre: 'Fiction', publish: 1987, edition: 2010 },
36+
{ title: 'Book Seven', genre: 'History', publish: 1986, edition: 1996 },
37+
{ title: 'Book Eight', genre: 'Science', publish: 2011, edition: 2016 },
38+
{ title: 'Book Nine', genre: 'Non-Fiction', publish: 1981, edition: 1989 },
39+
];
40+
41+
let userBooks= books.filter((bk) => bk.genre==="History")
42+
43+
userBooks= books.filter((bk) => {return bk.publish>1995 && bk.genre==="History"})
44+
45+
console.log(userBooks);

05_iterations/three.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// for of
2+
3+
//They are array specific loops
4+
// ["", "", ""]
5+
// [{}, {}, {}]
6+
//We may have to handle multiple strings in array or mu;ltiple objects from an array
7+
8+
const arr=[1, 2, 3, 4, 5]
9+
10+
for(const num of arr){
11+
//console.log(num);
12+
}
13+
14+
const greetings= "Hello World!"
15+
for(const greet of greetings){
16+
//console.log(`Each char is ${greet}`);
17+
}
18+
19+
//Maps
20+
21+
const map= new Map()
22+
map.set('IN', "India")
23+
map.set('USA', "United States Of America")
24+
map.set('JP', "Japan")
25+
map.set('Fr', "France")
26+
map.set('IN', "India")
27+
//console.log(map);
28+
29+
//Maps store unique values in same order as mentioned
30+
31+
// These are forof loop
32+
33+
for (const key of map) {
34+
console.log(key);
35+
}
36+
//Output:
37+
/*
38+
[ 'IN', 'India' ]
39+
[ 'USA', 'United States Of America' ]
40+
[ 'JP', 'Japan' ]
41+
[ 'Fr', 'France' ]
42+
*/
43+
for (const [key,value] of map) {
44+
console.log(key, ':-', value);
45+
}
46+
//Now the array is being destructured to give separate key, value pair
47+
48+
const myObject={
49+
game1: 'NFS',
50+
game2: 'Spiderman'
51+
}
52+
// for (const [key, value] of myObject) {
53+
// console.log(key, ':-', value);
54+
// }
55+
// The object myObject can't be iterated normal;y
56+

05_iterations/two.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//while
2+
3+
let index=0
4+
while(index<= 10){
5+
//console.log(`Value of index is ${index}`);
6+
index = index+2
7+
}
8+
9+
let myArray= ['flash', 'batman', 'superman']
10+
11+
let arr=0
12+
while(arr< myArray.length){
13+
console.log(`Value is ${myArray[arr]}`);
14+
arr=arr+1
15+
}
16+
17+
let score=11
18+
19+
do{
20+
console.log(`Score is ${score}`);
21+
score++
22+
}while(score<=10)
23+
24+
//It executes for the first time without checking the condition for the first time
25+

0 commit comments

Comments
 (0)