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
325 lines (278 loc) · 9.21 KB
/
practice.js
File metadata and controls
325 lines (278 loc) · 9.21 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// We'll consider a password's strength to be "strong" if it is at least 10
// characters long. If it's between 7 and 10 characters long, we'll consider it to
// have "medium" strength, and if it's less than 7 characters, we'll say it is a
// "weak" password. Write a function that accepts as input a potential password and
// returns either "weak", "medium" or "strong" depending on its length.
//
// passwordStrength("hello");
// //=> weak
//
// passwordStrength("longerpassword");
// //=> strong
//
// passwordStrength("helloone");
// //=> medium
var passwordStrength = function (newPassword) {
if (newPassword.length >= 10) {
return "strong";
}
else if (newPassword.length >= 8) {
return "medium";
}
else if (newPassword.length < 7) {
return "weak"
}
};
// A year is a leap year if it is divisible by 4, unless it is also divisible by
// 100 in which case it is not a leap year unless it is also divisible by
// 400. Phew. Got that? Good. Write a function that accepts a number and outputs
// true if the number is a leap year, false if not.
//
// isLeapYear(1988);
// //=> true
//
// isLeapYear(2001);
// //=> false
//
// isLeapYear(1800);
// //=> false
//
// isLeapYear(2000);
// //=> true
//
// It should also throw an error if the input is not a number.
//
// isLeapYear("hello");
// //=> THAT'S NOT A NUMBER!
var isLeapYear = function (year) {
if (typeof year !== "number") {
throw("THAT'S NOT A NUMBER!");
}
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
return true;
}
return false;
}
return true
}
console.log("before return false");
return false;
};
// Write a function that accepts three strings and input, and returns the one
// that would come earliest in the dictionary.
//
// firstInDictionary("rhino", "aardvark", "zebra");
// //=> aardvark
//
// firstInDictionary("whale", "zebra", "yak");
// //=> whale
//
// firstInDictionary("whale", "zebra", "aardvark");
// //=> aardvark
//
// It should throw an error string if any of the arguments are not strings.
//
// firstInDictionary("whale", 5, 10);
// //=> ALL THREE ARGS MUST BE STRINGS!
var firstInDictionary = function (first, second, third) {
if (typeof first !== "string" || typeof second !== "string" || typeof third !== "string") {
throw("All three must be strings!");
}
if (first < second && first < third) {
return first;
}
if (second < first && second < third) {
return second;
}
if (third < first && third < second) {
return third;
}
};
// Write a function that extracts a tag from a string representing an HTML
// element, but throws an error if the string is not an HTML element. You may reuse
// one of your functions from the previous section (or, better yet, see if you can
// remember how to re-write it).
//
// getTagName("<p>this is a paragraph</p>");
// //=> p
//
// getTagName("<p>this is wrong</div>");
// //=> Error: Not an HTML Element!
var getTagName = function (page) {
openTagStart = page.indexOf('<')
openTagEnd = page.indexOf('>', openTagStart)
openTag = page.slice(openTagStart + 1, openTagEnd)
bodyEnd = page.indexOf('<', openTagEnd + 1)
body = page.slice(openTagEnd + 1, bodyEnd)
closeTagStart = page.indexOf('<', bodyEnd)
closeTagEnd = page.indexOf('>', closeTagStart)
closeTag = page.slice(closeTagStart + 1, closeTagEnd)
closeTagMinusSlash = closeTag.slice(1, closeTagEnd)
if (openTag != closeTagMinusSlash)
{
throw("Error: Not an HTML Element!");
}
return openTag;
};
// Re-implement our improveTweet function so it can generate any of lol, omg,
// lmao, and rofl.
var improveTweet = function (tweet) {
choice = Math.round(Math.random() * 3)
switch (choice) {
case 0:
return tweet + " lol";
break;
case 1:
return tweet + " lmao";
break;
case 2:
return tweet + " omg";
break;
case 3:
return tweet + " rofl";
break;
}
};
// Write a function called `isQuestion` that returns true if the input is a
// string and it ends in a question mark. We'll use this function in the next
// practice problem.
var isQuestion = function (str) {
return (str.charAt(str.length - 1) === '?');
};
// The Magic 8 Ball is a classic toy that allows you to ask a yes/no
// question and it responds with a random answer. Most of the time (at least half)
// it responds with a "positive" answer, about a quarter of the time it responds
// with a "neutral" answer, and about a quarter of the time it responds with a
// "negative" answer. You can read more about the Magic 8 Ball toy (and see it's
// actual responses) on Wikipedia.
//
// Write a function that simulates the Magic 8 Ball by generating a random
// number. Try to make it match the probabilities of the real toy.
//
// magic8Ball("Will people like this problem?");
// //=> Very doubtful
//
// magic8Ball("Do people like these videos?");
// //=> My reply is no
//
// // throw an error if there's no question mark at the end
// // use your isQuestion function from the previous question
// magic8Ball("you suck");
// //=> THAT DOESN'T SOUND LIKE A QUESTION!
//
// magic8Ball("Is this a question?");
// //=> Signs point to yes
// Begin botch attempt!
// var magic8Ball = function (question) {
// if (! isQuestion(question))
// {
// throw("THAT DOESN'T SOUND LIKE A QUESTION!")
// }
// guess = Math.round(Math.random() * 100);
// console.log("guess = " + guess);
// switch (guess)
// {
// case (guess <= 50):
// console.log("Signs point to yes");
// return("Signs point to yes");
// break;
// case (guess <= 75):
// return("Very doubtful");
// break;
// case (guess > 75):
// return("My reply is no");
// break;
// }
var magic8Ball = function (question) {
if (! isQuestion(question))
{
throw("THAT DOESN'T SOUND LIKE A QUESTION!")
}
guess = Math.round(Math.random() * 100);
console.log("guess = " + guess);
if (guess <= 50)
{
return("Signs point to yes");
}
else if (guess <= 75)
{
return("My reply is no");
}
else if (guess > 75)
{
return("Very doubtful");
}
};
// Suppose we wanted to randomly interject "-lol-" or "-omg-" into a random
// place in a string. For example:
//
// randomInterject("this is a tweet");
// //=> this is -omg- a tweet
//
// Notice that it may break into the middle of words as well.
//
// randomInterject("hilarious, I'm having a great day");
// //=> hil-lol-arious, I'm having a great day");
//
// This is a little tricky, so here are some tips. It may be helpful to break down
// this problem into two steps. First, you may want to write a function called
// interjection that accepts a number and two strings -- the interjection and the
// actual string.
//
// interjectAt("interjection", 5, "hello world!");
// //=> hello-interjection- world!
//
// interjectAt("lol", 0, "this is a tweet");
// //=> -lol-this is a tweet
//
// interjectAt("omg", 15, "hello");
// //=> the string doesn't have that many letters!
//
// interjectAt(5, "omg", "hello");
// //=> expected first arg to be a string, second arg to be a number and third arg to be a string
//
// You can interject by using two slices. For instance:
//
// var str = "hello world!";
// var beginning = str.slice(0,5);
// var end = str.slice(5,str.length);
// var strWithInterjection = beginning + "-lol-" + end;
//
// You just have to generalize this to an arbitrary index and wrap it in a function.
var interjectAt = function (interjection, position, str) {
var dashedInterjection = "-" + interjection + "-";
var beginning = str.slice(0,position);
var end = str.slice(position, end);
if (typeof(position) !== 'number')
{
throw("Position must be an integer!");
}
console.log("interjection: " + interjection)
if (typeof(interjection) !== 'string')
{
throw("interjection must be a string!")
}
if (typeof(str) !== 'string')
{
throw("str must be a string!")
}
if (position > str.length)
{
throw("You can't interject outside of the string!");
}
return beginning + dashedInterjection + end
};
// Now that you have a robust function to do your interjection, your actual
// `randomInterjection` function consists of generating a random message and a
// random location within the string, and then calling into the `interjectAt`
// function with the appropriate arguments.
var randomInterject = function (tweet) {
var pos = Math.round(Math.random() * (tweet.length - 1));
console.log("pos: " + pos);
var choices = ['lol','omg'];
var whichInterjection = Math.round(Math.random() * (choices.length - 1));
console.log("whichInterjection: " + whichInterjection)
return interjectAt(choices[whichInterjection], pos, tweet);
};