-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-love.js
89 lines (70 loc) · 1.54 KB
/
js-love.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
function sum(term, a, next, b) {
return a > b ? 0 : term(a) + sum(term, next(a), next, b);
}
function identity(x) {
return x;
}
function inc(n) {
return n + 1;
}
function sum_integers(a, b) {
return sum(identity, a, inc, b);
}
// sum_integers(1, 100);
// 5050
// compute an approximation to π
function pi_sum(a, b) {
function pi_term(x) {
return 1 / (x * (x + 2));
}
function pi_next(x) {
return x + 4;
}
return sum(pi_term, a, pi_next, b);
}
// 8 * pi_sum(1, 5000);
// 3.141192653605793
class Person {
constructor(first, last) {
this.name = {
first,
last,
};
}
greeting() {
console.log(`Hi! I'm ${this.name.first}`);
}
}
class Teacher extends Person {
constructor(first, last, subject) {
super(first, last);
this.subject = subject;
}
greeting() {
console.log(`Hi! I'm ${this.name.first}. I teach ${this.subject}.`);
}
}
let han = new Person('Han', 'Solo');
// han.greeting();
// Hi! I'm Han
let snape = new Teacher('Severus', 'Snape', 'Dark arts');
// snape.greeting();
// Hi! I'm Severus. I teach Dark arts.
// function getValidEntries(entries) {
// return entries.filter((entry) => entry > 1);
// }
// // arr from somewhere else
// const arr = [1, 3, 5, 6];
// getValidEntries(arr);
// [ 3, 5, 6 ]
function getValidEntries(entries) {
return entries.filter((entry) =>
entries.type === 'special' ? entry > 5 : entry > 1
);
}
// arr from somewhere else
const arr = [1, 3, 5, 6];
// set the flag on the arr Object/Array
arr.type = 'special';
getValidEntries(arr);
// [ 6 ]