-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratchPad.js
More file actions
155 lines (139 loc) · 3.32 KB
/
scratchPad.js
File metadata and controls
155 lines (139 loc) · 3.32 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
function isBalanced(string) {
const arr = [];
for (let i = 0; i < string.length; i++) {
if (string[i] === '{') {
arr.push(string[i]);
} else if (string[i] === '}') {
const canPop = arr.pop();
if (!canPop) {
return false;
}
}
}
return arr.length === 0 ? true : false;
}
function fib(n) {
if (n === 0 || n === 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
const computedValues = [0, 1];
function fib(n) {
if (n >= 0) {
if (computedValues[n]) {
return computedValues[n];
}
computedValues[n] = fib(n - 1) + fib(n - 2);
return computedValues[n];
}
return 0;
}
function isBalanced2(string) {
const arr = [];
const lookupSymbols = {
'}': '{',
']': '[',
')': '(',
};
for (let i = 0; i < string.length; i++) {
if (string[i] === '{' || string[i] === '(' || string[i] === '[') {
arr.push(string[i]);
} else if (string[i] === '}' || string[i] === ')' || string[i] === ']') {
const canPop = arr.pop();
if (canPop !== lookupSymbols[string[i]]) {
return false;
}
}
}
return arr.length === 0 ? true : false;
}
function unique(array) {
const map = new Map();
const uniqArray = [];
for (let i = 0; i < array.length; i++) {
if (!map.get(array[i])) {
map.set(array[i], array[i]);
uniqArray.push(array[i]);
}
}
return uniqArray;
}
function intersection(arr1, arr2) {
const commonArray = [];
for (let i = 0; i < arr1.length; i++) {
map.set(arr1[i], arr[i]);
}
for (let i = 0; i < arr2.length; i++) {
if (map.get(arr2[i])) {
commonArray.push(arr2[i]);
}
}
return commonArray;
}
function includes(array, value) {
const middle = Math.floor(array.length / 2);
if (value === array[middle]) {
return true;
} else if (value > array[middle]) {
return includes(array.slice(middle + 1, array.length), value);
} else if (value < array[middle]) {
return includes(array.slice(0, middle), value);
}
return false;
}
function objectCopy(sourceObj, targetObj) {
for (let prop in sourceObj) {
if (typeof sourceObj[prop] === 'object') {
if (typeof targetObj[prop] !== 'object') {
targetObj[prop] = {};
}
objectCopy(sourceObj[prop], targetObj[prop]);
} else {
targetObj[prop] = sourceObj[prop];
}
}
}
function assignDeep(obj1, obj2) {
const copyObj = new Object();
objectCopy(obj1, copyObj);
objectCopy(obj2, copyObj);
return copyObj;
}
function reduce(array, reduceFunction, initialValue) {
let accumulator = 0;
for (let i = 0; i < array.length; i++) {
accumulator = reduceFunction(accumulator, array[i]);
}
return accumulator + initialValue;
}
async function reduceAsync(array, reduceFunction, initialValue) {
let accumulator = 0;
for (let i = 0; i < array.length; i++) {
const el = await(array[i]());
console.log(el);
accumulator = reduceFunction(accumulator, el);
}
return accumulator;
}
const sum = (x, y) => {
if (y) {
return x + y;
} else {
return (y) => x + y;
}
}
class Person {
constructor(name) {
this.x = name;
}
get name() {
return this.x.toUpperCase();
}
set name(newName) {
this.x = newName; // validation could be checked here such as only allowing non numerical values
}
walk() {
console.log(this.x + ' is walking.');
}
}