-
Notifications
You must be signed in to change notification settings - Fork 0
/
app8.js
83 lines (75 loc) · 2 KB
/
app8.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
function sum() {
let sum = 0;
for (let i = 1; i <= 5; i++) {
sum += i;
}
return sum;
}
console.log(sum());
let sum = 0;
for (let i = 5; i >= 1; i--) {
sum += i;
}
console.log(sum);
function sum(i) {
if (i == 1) {
return 1;
}
return i + sum(i - 1);
}
const result = sum(5);
console.log(result);
let factorial = 1;
for (let i = 5; i >= 1; i--) {
factorial *= i;
}
console.log(factorial);
function factorial(i) {
if (i == 1) {
return 1;
}
return i * factorial(i - 1);
}
// const result = factorial(5);
console.log(result);
/*
factorial(5) --> 5 * factorial(4)
factorial(4) --> 5 * 4 * factorial(3)
factorial(3) --> 5 * 4 * 3 * factorial(2)
factorial(2) --> 5 * 4 * 3 * 2 * factorial(1)
--> 5 * 4 * 3 * 2 * 1 (i == 1 --> return 1)
*/
function sum(num1, num2) {
console.log(num1, num2);
console.log(arguments);
console.log(arguments[2]);
}
sum(3, 4, 5, 6, 7);
const numbers = [10, 11, 12, 13, 14];
for (const number of numbers) {
console.log(number);
}
const products = [
{ id: 1, name: 'iPhone', brand: 'apple', price: 100000 },
{ id: 2, name: 'onePlus phone', brand: 'apple', price: 20000 },
{ id: 3, name: 'laptop f35', brand: 'apple', price: 360000 },
{ id: 4, name: 'tablet 19s', brand: 'apple', price: 40000 },
{ id: 5, name: 'nokia phone', brand: 'apple', price: 12000 },
{ id: 6, name: 'ipad 365', brand: 'apple', price: 51000 },
{ id: 7, name: 'Samsung s20', brand: 'apple', price: 95000 },
{ id: 8, name: 'Phone of 2023', brand: 'apple', price: 13500 }
]
function searchPhone(products, search) {
let matched = [];
for (const product of products) {
// console.log(product);
const productsName = product.name;
if (productsName.toLowerCase().includes(search.toLowerCase()) === true) {
// matched.push(productsName);
matched.push(product);
}
}
return matched;
}
const matchedProducts = searchPhone(products, 'phone');
console.log(matchedProducts);