-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.js
120 lines (111 loc) · 3.25 KB
/
arrays.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
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
// Given an array of numbers, return all the numbers that are greater than ten.
// Ex.:
// greaterThanTen([1, 2, 3, 11, 12, 13]);
// => [11, 12, 13]
function greaterThanTen(numbers) {
return numbers.filter((x) => x > 10)
}
// Given an array of strings, return all words that start with 'b' or 'B'.
// Ex.:
// bWords(['banana', 'orange', 'apple', 'Bonobo', 'kiwi', 'pear']);
// => ['banana', 'Bonobo]
function bWords(words) {
return words.filter((firstLett) => firstLett.charAt(0) === 'b' || firstLett.charAt(0) === 'B')
}
// Add all the elements from additionalItems to the end of originalArray.
// Return the originalArray..
// Ex.:
// extend([1, 2, 3], [4, 5, 6]);
// => [1, 2, 3, 4, 5, 6]
function extend(originalArray, additionalItems) {
for (const num of additionalItems) {
originalArray.push(num)
}
return originalArray
}
// Return an array of all items with the given length.
// Ex.:
// itemsWithLength(['a', 'bbb', 'cccc', 'dddddd', 'eee'], 3);
// => ['bbb', 'eee']
function itemsWithLength(items, length) {
return items.filter((string) => string.length === length)
}
// Return an array with every other element from the input array (start with index 0).
// Ex.:
// everyOtherItem(['a', 'b', 'c', 'd', 'e']);
// => ['a', 'c', 'e']
function everyOtherItem(items) {
return items.filter((evenIndex) => items.indexOf(evenIndex) % 2 === 0)
}
// Given a list of words and a letter, return the indexes of the words that
// start with that letter. You can assume that the words and letter will always
// be lowercased.
// Ex.:
// findWordsStartingWith(['apple', 'banana', 'kiwi', 'pear', 'bacon'], 'b');
// => [1, 4]
function findWordsStartingWith(words, letter) {
const newArr = []
for (let i = 0; i < words.length; i++) {
if (words[i].charAt(0) === letter) {
newArr.push(i)
}
}
return newArr
}
// Return the `n` smallest values in the array in descending order (largest
// numbers first). Assume that `n` will always be less than the length of the
// array.
// Ex.:
// smallestNItems([1, 30, 4, 21, 100000], 3);
// => [21, 4, 1]
function smallestNItems(items, n) {
let newArr = []
items.sort(function(a, b){return a - b});
for (let i = 0; i < n; i++) {
newArr.unshift(items[i])
}
return newArr
}
// Search for a value in the array and return its index. If the value appears
// more than once, return the index of the *FIRST* occurrence of the value. If
// the value doesn't exist in the array, return undefined.
// Ex.:
// findIndex(['a', 'b', 'c', 'a', 'b', 'c'], 'c');
// => 2
function findIndex(items, value) {
for (let i = 0; i < items.length; i++) {
if (items[i] === value) {
return i
}
}
}
// Given a start number and stop number, return a new array containing all the numbers
// between the start and stop number.
// Ex.:
// range(1, 5);
// => [1, 2, 3, 4, 5]
function range(start, stop) {
const newArr = [];
if (start > stop) {
for (let i = stop; i <= start; i++) {
newArr.push(i)
}
}
if (start < stop) {
for (let i = start; i <= stop; i++) {
newArr.push(i)
}
}
return newArr
}
export {
bWords,
everyOtherItem,
extend,
findIndex,
findWordsStartingWith,
greaterThanTen,
itemsWithLength,
range,
smallestNItems,
};