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
285 lines (252 loc) · 6.12 KB
/
practice.js
File metadata and controls
285 lines (252 loc) · 6.12 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Write a function called `isVowel` that returns `true` if the input is a
// single character and either an upper or lower-case a, e, i, o, or
// u. It should return false otherwise.
//
// isVowel("a");
// //=> true
//
// isVowel("E");
// //=> true
//
// isVowel(1);
// //=> false
//
// isVowel("Ea");
// //=> false
//
// isVowel("Y");
// //=> false
//
var isVowel = function (str) {
if (typeof(str) !== "string") {
return false;
}
var lstr = str.toLowerCase();
var vowels=['a','e','i','o','u'];
var vowel="";
for (var index=0; index < vowels.length; index++) {
vowel = vowels[index];
if (vowel === lstr) {
return true;
}
}
return false;
};
// Write a function called `isLowerCaseLetter` that returns `true` if
// the input is a single letter and lower-case. It should return false otherwise.
//
// isLowerCaseLetter("a");
// //=> true
//
// isLowerCaseLetter("A");
// //=> false
//
// isLowerCaseLetter(1);
// //=> false
//
// isLowerCaseLetter("ae");
// //=> false
//
// isLowerCaseLetter(true);
// //=> false
//
var isLowerCaseLetter = function (str) {
if (typeof str !== "string") {
return false;
}
if (str.length > 1) {
return false;
}
var lstr = str.toLowerCase();
if (str === lstr) {
return true;
}
return false;
};
// Write a function called `sumUpTo` so that it accepts a positive number `n`
// and sums the first `n` positive integers. For example, if you call `sumUpTo(5)`
// it should sum the numbers 1 through 5 inclusive. It should throw an error if the
// input number is negative.
// sumUpTo(100);
// //=> 5050
//
// sumUpTo(10);
// //=> 55
//
// sumUpTo(0);
// //=> 0
//
// sumUpTo(-10);
// //=> input must be a zero or a positive number!
//
var sumUpTo = function (upto) {
var sum=0;
if (upto < 0) {
throw "Input must be positive!";
}
for (var count=0; count< (upto + 1); count++) {
sum += count;
}
return sum;
};
// Write a function called `sumAToB` so that it accepts two parameters
// `a` and `b` and sums the numbers between `a` and `b` inclusive. Note
// that `a` may be larger than `b`, in which case you'll need to work
// backawards.
//
// sumAToB(10, 20);
// //=> 165
//
// sumAToB(0, -15);
// //=> 120
//
// sumAToB(-10, 500);
// //=> 125195
//
// sumAToB(10, 10);
// //=> 10
// sumAToB("hello", "world");
// //=> inputs should be numbers!
//
var sumAToB = function (a, b) {
var sum=0;
if (!(typeof a === "number" && typeof b === "number")) {
throw "A and B must be numbers!";
}
for (var count=a; count< (b + 1); count++) {
sum += count;
}
return sum;
};
// Write a function called `countVowels` that accepts a string and
// returns the number of vowels contained in that string. You'll want
// to use the `isVowel` helper function from above. It should throw an
// error if the input is not a string
//
// countVowels("hello world!");
// //=> 3
//
// countVowels("omg this is a hilarious tweet about nothing");
// //=> 16
//
// countVowels("");
// //=> 0
//
// countVowels(true);
// //=> input to countVowels must be a string!
//
var countVowels = function () {
};
// Write a function that accepts a string and returns the same string, only in
// reverse!
//
// reverse("hello world!");
// //=> !dlrow olleh
//
// reverse("omg this is a hilarious tweet about nothing");
// //=> gnihton tuoba teewt suoiralih a si siht gmo
//
// reverse("");
// //=>
//
// reverse(true);
// //=> input to reverseString must be an string!
//
var reverseString = function () {
};
// A number is a prime number if it is only evenly divisible by 1 and itself
// (although we don't consider 1 a prime number, so 2 is the first prime
// number). Write a function called `isPrime` that accepts a number `p` as an
// argument and returns `true` if it is prime, `false` otherwise.
//
// isPrime(101);
// //=> true
//
// isPrime(13);
// //=> true
//
// isPrime(1);
// //=> false
//
// isPrime(2);
// //=> true
//
// isPrime("hello");
// //=> false
//
// isPrime(-101);
// //=> false
//
var isPrime = function () {
};
// Using the `isPrime` function, write a function that accepts a number as
// input sums all the primes smaller than that number.
//
// sumPrimesUpTo(100);
// //=> 1060
//
// sumPrimesUpTo(0);
// //=> 0
//
// sumPrimesUpTo(1000);
// //=> 76127
//
// sumPrimesUpTo(2);
// //=> 2
//
// sumPrimesUpTo("whatever");
// //=> input should be a number
//
var sumPrimesUpTo = function () {
};
// Using the `isPrime` function, write a function that takes in a
// positive integer, n, and returns the first n numbers.
//
// sumOfFirstNPrimes(10);
// //=> 129
//
// sumOfFirstNPrimes(100);
// //=> 24133
//
// sumOfFirstNPrimes(1000);
// //=> 3672913
//
// sumOfFirstNPrimes(0);
// //=> 0
//
// sumOfFirstNPrimes(1);
// //=> 2
//
// sumOfFirstNPrimes(-10);
// //=> input number should be zero or a positive number!
//
var sumOfFirstNPrimes = function () {
};
// A _palindrome_ is a string that reads the same forwards and backwards. Write
// a function that accepts a string of arbitrary letters, numbers, and symbols, and
// returns true if it's a palindrome. For example:
//
// isPalindrome("kayak");
// //=> true
//
// isPalindrome("A man, a plan, a canal, Panama");
// //=> true
//
// isPalindrome("hello world");
// //=> false
//
// Let's start by writing a function to remove all non-letter characters
// from the input.
//
// removeNonLetters("A man, a plan, a canal, Panama");
// //=> AmanaplanacanalPanama
//
// removeNonLetters("this is a string; it has some punctuation!");
// //=> thisisastringithassomepunctuation
//
var removeNonLetters = function () {
};
// Now use `removeNonLetters`, along with the `reverse` function from above to
// determine if the string is a palindrome.
var isPalindrome = function () {
};