Repository navigation
Expand file tree
/
Copy pathjava-loops.js
More file actions
135 lines (113 loc) · 3.2 KB
/
Copy pathjava-loops.js
File metadata and controls
135 lines (113 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// loops in java
//for loops
const array = [1,2,3,4,5,6]
for (let i = 0; i < array.length; i++) {
//console.log(array[i]);
}
//break and continue
for (let i = 0; i < array.length; i++) {
const element = array[i];
if (element === 2) {
continue;
}
//console.log(element);
if (element === 4) {
//console.log(`${element} has come up. goodbye :/`);
break
}
}
// WHILE loops
let i=0;
while (i<10) {
//console.log(i);
i++
}
i=0;
do {
i++;
//console.log(i);
} while (i<5);
//SPECIAL LOOPS
//for of
const User = {
name: "henry",
age: 18
}
const arr = ["hi","what's","up"]
for (const num of arr) {
//console.log(num);
}
const newmap = new Map()
newmap.set('BR',"Brazil")
newmap.set('CR',"Croatia")
newmap.set('DE',"Germany")
//maps only have unique values
//printing maps
for (const [key,value] of newmap) {
//console.log(key + ' '+ value);
}
//forin loop (objects)
for (const key in User) {
if (!Object.hasOwn(User, key)) continue;
// console.log(User[key]);
}
//note: if we use for in loop using arrays, keys store indexes of the array.
// therefore, for of loop is used for arrays.
//IMPORTANT - foreach loop
const coding = ["js","ruby","swift"]
coding.forEach((item) => {
//console.log(item);
})
// if it was an array of objects
coding.forEach((item) => {
//console.log(item.key);
})
// NOTE: Other functions defined outside the ForEach loop can also be used. But they have to be referenced.
// filtering arrays
const nums = [1,2,3,9,10]
const smallnums = nums.filter( (num) => num < 5)
// console.log(smallnums);
// if scope is opened ({}) , then return keyword is necessary.
const books = [
{ title: 'Book One', genre: 'Fiction', publish: 1981, edition: 2004 },
{ title: 'Book Two', genre: 'Non-Fiction', publish: 1992, edition: 2008 },
{ title: 'Book Three', genre: 'History', publish: 1999, edition: 2007 },
{ title: 'Book Four', genre: 'Non-Fiction', publish: 1989, edition: 2010 },
{ title: 'Book Five', genre: 'Science', publish: 2009, edition: 2014 },
{ title: 'Book Six', genre: 'Fiction', publish: 1987, edition: 2010 },
{ title: 'Book Seven', genre: 'History', publish: 1986, edition: 1996 },
{ title: 'Book Eight', genre: 'Science', publish: 2011, edition: 2016 },
{ title: 'Book Nine', genre: 'Non-Fiction', publish: 1981, edition: 1989 },
];
const scbooks = books.filter( (b) => b.publish < 2000)
scbooks.forEach((b) => {
//console.log(b.title);
})
//map property of arrays
let nums1 = [1,2,3,4,5]
nums1 = nums1.map((i) => i + 10)
// chaining = applying multiple properties (same or different) to an array
nums1=nums1.map((num) => num+10).filter((num) => num >22)
// console.log(nums1);
//reduction
const shoppingCart = [
{
itemName: "js course",
price: 2999
},
{
itemName: "py course",
price: 999
},
{
itemName: "mobile dev course",
price: 5999
},
{
itemName: "data science course",
price: 12990
},
]
const initial = 0
const total = shoppingCart.reduce((acc,item) => acc + item.price,0)
console.log(`hi, your total today is ${total} unfortunately :>`);