This repository was archived by the owner on Dec 27, 2019. It is now read-only.
forked from semmypurewal/BeginningJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.js
More file actions
201 lines (185 loc) · 6.28 KB
/
practice.js
File metadata and controls
201 lines (185 loc) · 6.28 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
201
// Write a function called `containsTwice` that accepts a number and an array,
// and returns `true` if that number appears in the array twice, and `false`
// otherwise.
//
// containsTwice(5, [1, 2, 3, 4, 5]);
// //=> false
//
// containsTwice("hello", [ "hello", "world", "hello" ]);
// //=> true
//
// containsTwice(true, [ true, false, false, true ]);
// //=> true
//
// containsTwice(10, [10, 10, 10, 10, 10]);
// //=> false
//
var containsTwice = function () {
};
// Generalize the previous solution into a function called `containsNTimes` so
// that it can check for a value an arbitrary number of times.
//
// containsNTimes(3, "hello", [ "hello", "hello", "hello" ]);
// //=> true
//
// containsNTimes(5, true, [ true, true, true, true, false ]);
// //=> false
//
// containsNTimes(0, 5, [ 1, 2, 3, 4, 5 ]);
// //=> false
//
var containsNTimes = function () {
};
// Write a function called `atLeastOneEven` that returns `true` if at least one of
// the numbers in input array is even, false otherwise. It should throw an error if
// the argument is not an array.
//
// atLeastOneEven([ 3, 5, 6, 7, 9 ]);
// //=> true
//
// atLeastOneEven([]);
// //=> false
//
// atLeastOneEven([ 101, 203, 401 ]);
// //=> false
//
// atLeastOneEven("hello");
// //=> input should be an array!
//
var atLeastOneEven = function () {
};
// Write a function called `allStrings` that accepts an array as an argument and
// returns `true` if all of the values in the array are strings. It should
// return false if they are not all strings, and throw an error if the input is
// not an array.
//
// allStrings([ "these", "are", "all", "strings" ]); //=> true
//
// allStrings([ "these", "are", "not", 5 ]); //=> false
//
// allStrings([ ]); //=> true
//
// allStrings("hello"); //=> input should be an array!
//
// Although the tests will not be checking for this, try to make your loop exit
// as soon as it finds a non-string entry in the array.
//
var allStrings = function () {
};
// Write a function that accepts two arrays, and returns true if any of the
// values in the first array appear twice in the second array. You might want to
// reuse the function `containsNTimes` or `containsTwice` from above. It should
// throw an error if either of the inputs are not arrays.
//
//
// containsAnyTwice([1, 2], ["hello", 1, "world", 1]);
// //=> true
//
// containsAnyTwice([], ["always", "will", "return", "false"]);
// //=> false
//
// containsAnyTwice(["hello", "world"], ["hello", "hello", "world", "world"]);
// //=> true
//
// containsAnyTwice("hello", ["hello", "world"]);
// //=> containsAnyTwice expects two arguments, both of which should be an array.
//
// Although the tests will not be checking for this, try to make your loop exit
// as soon as it finds an element in the first array that appears twice in the second
// array.
//
var containsAnyTwice = function () {
};
// In the previous problem, we determined whether or not an array contains any
// of a list of values exactly twice. In this problem, we'll actually return
// those values appearing twice. Create a new function that returns an array of
// only the values from the first array that appear twice in the second array.
//
// For this problem, you'll create a new array, and you'll use its `push`
// function to add elements to the end. You'll most likely want to use the
// `containsTwice` function you created in the previous exercise.
//
// The difficulty here will be in avoiding duplicates. You may want to use the
// `indexOf` method of the resulting array to confirm that you're not adding a
// value a second time.
//
// getValuesAppearingTwice(["hello", 1, "world", 1]);
// //=> [ 1 ]
//
// getValuesAppearingTwice(["always", "will", "return", "empty"]);
// //=> []
//
// getValuesAppearingTwice(["hello", "hello", "world", "world", "goodbye"]);
// //=> [ "hello", "world" ]
//
// getValuesAppearingTwice(["hello", "world", "goodbye"])
// //=> []
//
var getValuesAppearingTwice = function () {
};
// Using a standard `for` loop, along with the `push` function, write a function
// called `range` that accepts two numbers, `begin` and `end`, and returns an array
// that contains all of the integers starting at `begin` and ending at `end`
// (including `begin` and `end`). For example:
//
// range(5,10);
// //=> [5, 6, 7, 8, 9, 10]
//
// range(0,3);
// //=> [0, 1, 2, 3]
//
// range(10,3);
// //=> [10, 9, 8, 7, 6, 5, 4, 3]
//
// It should throw an error when either of the arguments are not numbers.
//
// range("hello", "world");
// //=> arguments to range must be numbers
//
var range = function () {
};
// Using the `isHTMLElement` and `getTagName` function from one of the previous
// sections, write a function called `mapToTags` that accepts an array of HTML
// elements and returns a new array that consists of only the tags associated with
// those HTML elements. It should throw an error if any of the elements are not
// HTML elements, or if the input is not an array.
//
// mapToTags(["<p>this is a paragraph</p>", "<span>this is a span</span>", "<li>this is a list item</li>"]);
// //=> ["p", "span", "li"]
//
// mapToTags([]);
// //=> []
//
// mapToTags(["<p>this is a paragraph</p>", "this is a tweet"]);
// //=> "this is a tweet" is not an HTML element!
//
// mapToTags(5);
// //=> wat?
//
// mapToTags([ "not an html element" ]);
// //=> all entries must be html elements!
//
var mapToTags = function () {
};
// Write a function called `filterToLol` which accepts an array of tweets and
// returns an array that consists only of those that contain `lol` (upper, lower,
// or mixed-case). It should throw an error if the input is not an array or if any
// of the elements are not strings.
//
// filterToLol(["hello world!", "this is a tweet lol", "this is a tweet omg"]);
// //=> ["this is a tweet lol"]
//
// filterToLol(["lol", "LOL", "LoL", "omg", "lollerskates"]);
// //=> ["lol", "LOL", "LoL", "lollerskates"]
//
// filterToLol(["omg", "this is a tweet"]);
// //=> []
//
// filterToLol(5);
// //=> no can do.
//
// filterToLol(["this is a string", false, 5]);
// //=> all entries must be strings!
//
var filterToLol = function () {
};