Skip to content

Commit 98c78a2

Browse files
committed
loops discussion
1 parent 1158d68 commit 98c78a2

File tree

8 files changed

+274
-0
lines changed

8 files changed

+274
-0
lines changed

05_iterations/eight.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//reduce
2+
3+
const myNums = [1, 2, 3]
4+
5+
// const myTotal = myNums.reduce(function (acc, currval) {
6+
// console.log(`acc: ${acc} and currval: ${currval}`);
7+
// return acc + currval
8+
// }, 0)
9+
10+
const myTotal = myNums.reduce( (acc, curr) => acc+curr, 0)
11+
12+
console.log(myTotal);
13+
14+
15+
const shoppingCart = [
16+
{
17+
itemName: "js course",
18+
price: 2999
19+
},
20+
{
21+
itemName: "py course",
22+
price: 999
23+
},
24+
{
25+
itemName: "mobile dev course",
26+
price: 5999
27+
},
28+
{
29+
itemName: "data science course",
30+
price: 12999
31+
},
32+
]
33+
34+
const priceToPay = shoppingCart.reduce((acc, item) => acc + item.price, 0)
35+
36+
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+
//for each
2+
3+
const coding = ["js", "ruby", "java", "python", "cpp"]
4+
5+
// coding.forEach( function (val){
6+
// console.log(val);
7+
// } )
8+
9+
// coding.forEach( (item) => {
10+
// console.log(item);
11+
// } )
12+
13+
// function printMe(item){
14+
// console.log(item);
15+
// }
16+
17+
// coding.forEach(printMe)
18+
19+
// coding.forEach( (item, index, arr)=> {
20+
// console.log(item, index, arr);
21+
// } )
22+
23+
const myCoding = [
24+
{
25+
languageName: "javascript",
26+
languageFileName: "js"
27+
},
28+
{
29+
languageName: "java",
30+
languageFileName: "java"
31+
},
32+
{
33+
languageName: "python",
34+
languageFileName: "py"
35+
},
36+
]
37+
38+
myCoding.forEach( (item) => {
39+
40+
console.log(item.languageName);
41+
} )

05_iterations/four.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//for in
2+
3+
const myObject = {
4+
js: 'javascript',
5+
cpp: 'C++',
6+
rb: "ruby",
7+
swift: "swift by apple"
8+
}
9+
10+
for (const key in myObject) {
11+
//console.log(`${key} shortcut is for ${myObject[key]}`);
12+
}
13+
14+
const programming = ["js", "rb", "py", "java", "cpp"]
15+
16+
for (const key in programming) {
17+
//console.log(programming[key]);
18+
}
19+
20+
// const map = new Map()
21+
// map.set('IN', "India")
22+
// map.set('USA', "United States of America")
23+
// map.set('Fr', "France")
24+
// map.set('IN', "India")
25+
26+
// for (const key in map) {
27+
// console.log(key);
28+
// } // for in wont run in maps

05_iterations/one.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// for loop
2+
3+
for (let i = 0; i <= 10; i++) {
4+
const element = i;
5+
if (element == 5) {
6+
//console.log("5 is best number");
7+
}
8+
//console.log(element);
9+
10+
}
11+
12+
// console.log(element); //not accesible
13+
14+
for (let i = 1; i <= 10; i++) {
15+
//console.log(`Outer loop value: ${i}`);
16+
for (let j = 1; j <= 10; j++) {
17+
//console.log(`Inner loop value ${j} and inner loop ${i}`);
18+
//console.log(i + '*' + j + ' = ' + i*j );
19+
}
20+
21+
}
22+
let myArray = ["flash", "batman", "superman"]
23+
//console.log(myArray.length);
24+
for (let index = 0; index < myArray.length; index++) {
25+
const element = myArray[index];
26+
//console.log(element);
27+
28+
}
29+
30+
31+
32+
// break and continue
33+
34+
// for (let index = 1; index <= 20; index++) {
35+
// if (index == 5) {
36+
// console.log(`Detected 5`);
37+
// break
38+
// }
39+
// console.log(`Value of i is ${index}`);
40+
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+
50+
}

05_iterations/seven.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const myNumers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2+
3+
// const newNums = myNumers.map( (num) => { return num + 10})
4+
5+
const newNums = myNumers
6+
.map((num) => num * 10 )
7+
.map( (num) => num + 1)
8+
.filter( (num) => num >= 40)
9+
10+
console.log(newNums);

05_iterations/six.js

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

05_iterations/three.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// for of
2+
3+
// ["", "", ""]
4+
// [{}, {}, {}]
5+
6+
const arr = [1, 2, 3, 4, 5]
7+
8+
for (const num of arr) {
9+
//console.log(num);
10+
}
11+
12+
const greetings = "Hello world!"
13+
for (const greet of greetings) {
14+
//console.log(`Each char is ${greet}`)
15+
}
16+
17+
// Maps
18+
19+
const map = new Map()
20+
map.set('IN', "India")
21+
map.set('USA', "United States of America")
22+
map.set('Fr', "France")
23+
map.set('IN', "India")
24+
25+
26+
// console.log(map);
27+
28+
for (const [key, value] of map) {
29+
// console.log(key, ':-', value);
30+
}
31+
32+
const myObject = {
33+
game1: 'NFS',
34+
game2: 'Spiderman'
35+
}
36+
37+
// for (const [key, value] of myObject) {
38+
// console.log(key, ':-', value);
39+
40+
// } //for of wont run for objects, not iterable like this

05_iterations/two.js

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

0 commit comments

Comments
 (0)