-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30daysOfJavaScript.html
More file actions
139 lines (111 loc) · 3.92 KB
/
30daysOfJavaScript.html
File metadata and controls
139 lines (111 loc) · 3.92 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCode - 30 Days of JavaScript</title>
</head>
<body>
<h1>30 Days of JavaScript</h1>
<script>
//2667. Create Hello World Function
// const createHelloWorld = function() {
// return function(...args) {
// return "Hello World";
// }
// };
//2620. Counter
// function createCounter(n) {
// let counterValue = n;
// function counter() {
// return counterValue++;
// }
// return counter;
// }
//2704. To Be Or Not To Be
// const expect = function (initialValue) {
// return {
// toBe: function (val) {
// if (initialValue === val) {
// return true;
// } else {
// throw new Error("Not Equal");
// }
// },
// notToBe: function (val) {
// if (initialValue !== val) {
// return true;
// } else {
// throw new Error("Equal");
// }
// }
// };
// };
//Counter II
// const createCounter = function (init) {
// let count = init;
// let numArray = [];
// return {
// increment: function () {
// count++;
// numArray.push(count);
// return (count);
// },
// decrement: function () {
// count--;
// numArray.push(count);
// return (count);
// },
// reset: function () {
// count = init;
// numArray.push(count);
// return (count);
// }
// }
// }
//Apply Transofmr Over Each Element in Array
//given input is an array, and a function
//return a new array with a transformation to each element
//returnedArray[i] = fn(arr[i], i)
//do not use Array.map
// const map = function (arr, fn) {
// for (let i = 0; i < arr.length; i++) {
//apply the function to each value in the array
//push values into a new array
// console.log(arr[i]);
// arr[i] = fn(arr[i], i);
// }
// return arr;
// };
// map([1,2,3]);
//Filter Elements from Array
//given an integer array []
//given a filtering function fn(arr[i], i)
//return a filtered array []
// const filter = function(arr, fn) {
// let filteredArr = [];
// for (let i = 0; i < arr.length; i++) {
// if (fn(arr[i], i) === true || fn(arr[i], i) != 0) {
// filteredArr.push(arr[i])
// }
// }
// return filteredArr;
// };
//Array Reduce Transformation
//given: nums [integer array], fn (reducer function), init (initial value)
const reduce = function(nums, fn, init) {
if (nums.length === 0) {
return init;
}
for (let i = 0; i < nums.length; i++) {
let val = fn(init, nums[i]);
init = val;
console.log(init);
}
return init
};
function sum(accum, curr) { return accum + curr; }
reduce([1, 2, 3, 4], sum, 0)
</script>
</body>
</html>