-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
200 lines (174 loc) · 3.78 KB
/
script.js
File metadata and controls
200 lines (174 loc) · 3.78 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use strict";
// 100 Days of CODEWARS
// SUM ARRAYS
function sum(numbers) {
if (numbers.length === 0) {
return 0;
}
const arrSum = numbers.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
return arrSum;
}
// SUM MULTI-DIMENSION ARRAY
function arrayPlusArray(arr1, arr2) {
const arr3 = arr1
.concat(arr2)
.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
return arr3;
}
console.log(arrayPlusArray([-1, -2, -3], [-4, -5, -6]));
function sumOfTrippledEvens(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
// check if for even no.
if (array[i] % 2 === 0) {
// multiply evens by 3
const trippledEvenNumber = array[i] * 3;
// add new no. to the sum
sum += trippledEvenNumber;
}
}
return sum;
}
console.log(sumOfTrippledEvens([2, 1, 5, 6, 8]));
const arr = [1, 2, 3, 4, 5];
const productOfAllNums = arr.reduce((total, currentItem) => {
return total * currentItem;
});
console.log(productOfAllNums);
console.log(arr);
// for loop
const books = [
{
title: "Book",
author: "Name",
},
{
title: "Book2",
author: "Name2",
},
];
function getTheTitles(array) {
const titles = [];
for (let i = 0; i < array.length; i++) {
titles.push(array[i].title);
}
return titles;
}
console.log(getTheTitles(books));
// Using .map
const book = [
{
title: "Book1",
author: "Name",
},
{
title: "Book2",
author: "Name2",
},
{
title: "Book3",
author: "Name2",
},
{
title: "Book4",
author: "Name2",
},
];
function getTheTitles(array) {
return array.map((book) => book.title);
}
console.log(getTheTitles(book));
// CALCULATOR
// Add
const add = function (num1, num2) {
return num1 + num2;
};
// Subtract
const subtract = function (num1, num2) {
return num1 - num2;
};
// Get the sum
const sumOf = function (array) {
return array.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
};
// Multiplication
const multiply = function (array) {
return array.reduce(
(accumulator, currentValue) => accumulator * currentValue
);
};
// Power
const power = function (num1, num2) {
return Math.pow(num1, num2);
};
// Factorial
const factorial = function (n) {
if (n >= 1) {
return n * factorial(n - 1);
} else return 1;
};
console.log(factorial(4));
// Fibonacci
const fibonacci = function (n) {
if (n >= 3) {
return fibonacci(n - 1) + fibonacci(n - 2);
} else return 1;
};
console.log(fibonacci(8));
// Palindromes
const palindrome = function (string) {
const cleanedString = string.toLowerCase().replace(/[^a-z0-9]/g, "");
console.log(cleanedString);
return cleanedString === cleanedString.split("").reverse().join("");
};
console.log(palindrome("nurses run"));
// Find The Oldest
const people = [
{
name: "Carly",
yearOfBirth: 1942,
yearOfDeath: 1970,
},
{
name: "Ray",
yearOfBirth: 1962,
yearOfDeath: 2011,
},
{
name: "Jane",
yearOfBirth: 1912,
yearOfDeath: 1941,
},
];
const findTheOldest = function (array) {
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest);
const currentAge = getAge(currentPerson);
return oldestAge > currentAge ? oldest : currentPerson;
});
};
function getAge(person) {
if (!person.yearOfDeath) {
const currentYear = new Date().getFullYear();
return currentYear - person.yearOfBirth;
} else return person.yearOfDeath - person.yearOfBirth;
}
console.log(findTheOldest(people));
// Two Sum
function twoSum(array, target) {
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] + array[j] === target) {
let output = [i, j];
return output;
}
}
}
}
console.log(twoSum([1, 2, 3], 4));