-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnJavaScript.js
More file actions
182 lines (129 loc) · 3.96 KB
/
Copy pathLearnJavaScript.js
File metadata and controls
182 lines (129 loc) · 3.96 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//make the CamelCase from list-style-image == listStyleImage
function camelize(str) {
return str
.split('-')
.map(
(word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1)
)
.join('');
}
console.log(camelize("list-style-image"))
// finds the number between the provided nums a b
let arr = [5, 3, 8, 1];
function filterRange(str, a, b) {
const filtered = str.filter(item => (item >= a && item <= b));
return filtered;
}
console.log(filterRange(arr, 1, 4))
//deleting numbers that are not in scope betwee provided nums a and b from array
function filterRangeInPlace(arr, a, b) {
for(let i = 0; i < arr.length; i++){
if(arr[i]<a || arr[i]>b){
arr.splice(i, 1);
i--;
}
}
}
console.log(filterRangeInPlace(arr, 1, 4))
console.log("Arr: ", arr)
let arr1 = [5, 2, 1, -10, 8];
//sorting the array from biggest to lowest
const ArraySort = (arr) => {
return arr.sort((a, b) => b - a);
}
console.log(ArraySort(arr1))
console.log(arr1)
//sort the array with copy
const arr2 = ["HTML", "JavaScript", "CSS"]
const copySort = (arr) => {
const sorted = []
arr2.map(item => sorted.push(item));
return sorted.sort();
}
console.log("Sorted Copy Array: ", copySort())
console.log(arr2)
function Calculator(name){
this.methods = {
"-": (a, b) => a - b,
"+": (a, b) => a + b
};
this.calculate = (str) =>{
let arr = str.split(" ");
let a = +arr[0];
let b = +arr[2];
let op = arr[1];
return this.methods[op](a, b)
}
this.addMethod = (name, func) => {
this.methods[name]=func;
}
}
let calc1 = new Calculator("John");
console.log(calc1.calculate("3 + 7"))
calc1.addMethod("/", (a, b) => a / b)
calc1.addMethod("*", (a, b) => a * b)
calc1.addMethod("**", (a, b) => Math.pow(a,b))
console.log("6 / 2 = ", calc1.calculate("6 / 2"))
console.log("6 * 2 = ", calc1.calculate("6 * 2"))
console.log("2 ^ 3 = ", calc1.calculate("2 ** 3"))
//Make the array of names out of the array of the object
let vasyl = { name: "Vasyl", age: 25 };
let piotr = { name: "Piotr", age: 30 };
let michal = { name: "Michał", age: 28 };
let users = [ vasyl, piotr, michal ];
let names = users.map(item => item.name);
console.log(names)
alert( names );
//make the array with full name and
let arni = { name: "Arnold", surname: "Schwarcneger", id: 1 };
let jason = { name: "Jason", surname: "Statham", id: 2 };
let adam = { name: "Adam", surname: "Mickiewicz", id: 3 };
let users1 = [ arni, jason, adam ];
let usersMapped = users1.map(item => {return {"id": item.id, "fullName": `${item.name} ${item.surname}`}})
/*
usersMapped = [
{ fullName: "Arnold Schwarcneger", id: 1 },
{ fullName: "Jason Statham", id: 2 },
{ fullName: "Adam Mickiewicz", id: 3 }
]
*/
alert( usersMapped[0].id )
alert( usersMapped[0].fullName )
//sort the user by age
const sortByAge = (arr) =>{
return arr.sort((a, b) => a.age -b.age)
//arr.sort((a, b) => a.age > b.age ? 1 : -1);
}
let arn = { name: "Arnold", age: 25 };
let jas = { name: "Jason", age: 30 };
let adm = { name: "Adam", age: 28 };
let arr3 = [ arn, jas, adm ];
console.log(sortByAge(arr3));
// after sort
// alert(arr[0].name); // Arnold
// alert(arr[1].name); // Adam
// alert(arr[2].name); // Jason
//Random sort
arr4 = [1, 2, 3];
function randomSort(arr) {
return arr.sort(() => Math.random() -0.5);
}
//get average age
const getAverage = (arr) => {
return Math.round(arr.reduce((sum, next) => sum + next.age, 0) / arr.length);
}
console.log(getAverage(arr3))
//return unique elements of the array
function unique(arr) {
let result = [];
for (let str of arr) {
if (!result.includes(str)) {
result.push(str);
}
}
return result;
}
let strings = ["Hello", "Hello", "world", "world",
"Hi", "CapeTown", "world", "Hello", ":-O"
];
console.log("Unique: ", unique(strings) ); // Hi CapeTown :-O