-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·166 lines (147 loc) · 3.08 KB
/
index.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//Name of City
function nameOfCity(cityName) {
const firstThreeChr = cityName.substring(0, 3).toLowerCase();
if (firstThreeChr == "new" || firstThreeChr == "los") {
return cityName;
} else {
return `the city name it's not `;
}
}
console.log(nameOfCity("Valletta"));
// Is Divisible
const isDivisible = (num) => num % 100 == 0;
console.log(isDivisible(1000));
// Ternary Operator
const isRaining = (bool) =>
bool
? "wet day - you need an umbrella!"
: "dry day - leave your umbrella at home!";
console.log(isRaining(true));
// Loop Sequence
const geometricalSequence = () => {
let str = "";
for (let i = 1; i <= 256; ) {
// if (i == 1) {
// str += i;
// i += i;
// continue;
// }
str += `${i} `;
i += i;
// i+= i -> i + i
}
return str.trim();
};
console.log(geometricalSequence());
// Multiples of Three
function multiplesOfThree() {
let str = "";
for (let i = 1; i <= 15; i++) {
if (i % 3 == 0) {
str += `${i} `;
}
}
return str.trim();
}
console.log(multiplesOfThree());
// powerOf
function powerOf(integer) {
return Math.pow(integer, integer);
// return integer ** integer
}
console.log(powerOf(5));
// Problem Solving: Number of vowels
function vowelCount(string) {
const vo = "aeiou";
const str = string.toLowerCase();
let count = 0;
for (let i = 0; i < string.length; i++) {
for (let j = 0; j < vo.length; j++) {
if (str[i] == vo[j]) {
count++;
}
}
}
return count;
}
console.log(vowelCount("test"));
// Objects
const obj1 = {};
obj1["name"] = "Olga";
console.log(obj1);
const obj2 = {
userName: "Hadi",
};
console.log(obj2);
const obj3 = new Object();
const person = {
userName: "Zain",
age: 22,
add: "Berlin",
};
const arr = Object.entries(person);
console.log(arr);
// [[],[],[],[]]
const arrOfArr = [
[33, 2],
[22, 3],
[44, 11],
[51, 55],
];
console.log(arrOfArr[2][1]);
//
const objects = [
{ name: "Nancy", lasName: "Zo" },
{
name: "Olga",
lastName: "do",
printOut: function () {
console.log("Hi");
},
},
];
//objects[1].printOut();
// other way to acc
objects[1]["printOut"]();
// Clones vs. References
// Primitive (Simple values are always clones)
// String, Number, Boolean
let x = 3;
let z = x; // clone coz it's simple
console.log(z);
z = 55;
console.log(x);
let strLong = "Hi, I am here";
let cloneStr = strLong; // clone coz it's simple
cloneStr = "Noooooooooo";
console.log(strLong);
// Object
const car = {
name: "Ford",
year: "2020",
color: "Red",
};
// Objects are references by default: Array and object literals
const newCar = car; // ref
newCar.name = "BMW";
console.log(car);
const names = ["Olga", "Zain", "Jack", "Nancy"];
// 1
const namesClone = [...names];
namesClone[0] = "Hadi";
console.log(names);
// 2
const newCloneArr = [].concat(names);
console.log(newCloneArr);
// 3
const lastWay = names.slice(0);
lastWay[0] = "cool";
console.log(lastWay);
const geometricalSequence1 = () => {
let result;
for (let i = 0; i < 9; i * 2) {
result += i;
}
return result;
};
console.log(geometricalSequence1());