Welcome! These are some bonus puzzles to accompany the Girl Develop It JavaScript for Beginners class. Please take a look at the puzzles, and work on one that looks challenging but not completely overwhelming. You can always reference the slides if you get stuck. Commit to spend at least 20 minutes trying to figure out a problem before you look at the answers. It helps you learn!
33 | 34 |Class 1
35 |The Fortune Teller
36 |Why pay a fortune teller when you can just program your fortune yourself?
37 |-
38 |
- Store the following into variables: number of children, partner's name, geographic location, job title. 39 |
- Output your fortune to the screen like so: "You will be a X in Y, and married to Z with N kids." 40 |
45 | var numKids = 5;
46 | var partner = 'David Beckham';
47 | var location = 'Costa Rica';
48 | var jobTitle = 'web developer';
49 |
50 | var future = 'You will be a ' + jobTitle + ' in ' + location + ', and married to ' +
51 | partner + ' ' + ' with ' + numKids + ' kids.';
52 | console.log(future);
53 |
54 | The Calculator
57 |-
58 |
- Write a function called
squareNumber
that will take one argument (a number), square that number, and return the result. It should also log a string like "The result of squaring the number 3 is 9."
59 | - Write a function called
halfNumber
that will take one argument (a number), divide it by 2, and return the result. It should also log a string like "Half of 5 is 2.5.".
60 | - Write a function called
percentOf
that will take two numbers, figure out what percent the first number represents of the second number, and return the result. It should also log a string like "2 is 50% of 4."
61 | - Write a function called
areaOfCircle
that will take one argument (the radius), calculate the area based on that, and return the result. It should also log a string like "The area for a circle with radius 2 is 12.566370614359172." 62 |- Bonus: Round the result so there are only two digits after the decimal.
64 | - Write a function that will take one argument (a number) and perform the following operations, using the functions
65 | you wrote earlier:
66 |
-
67 |
- Take half of the number and store the result. 68 |
- Square the result of #1 and store that result. 69 |
- Calculate the area of a circle with the result of #2 as the radius. 70 |
- Calculate what percentage that area is of the squared result (#3). 71 |
73 |
78 | function squareNumber(num) {
79 | var squaredNumber = num * num;
80 | console.log('The result of squaring the number ' + num + ' is ' + squaredNumber);
81 | return squaredNumber;
82 | }
83 |
84 | squareNumber(3);
85 |
86 | function halfOf(num) {
87 | var half = num / 2;
88 | console.log('Half of ' + num + ' is ' + half);
89 | return half;
90 | }
91 |
92 | halfOf(5);
93 |
94 | function percentOf(num1, num2) {
95 | var percent = (num1/num2) * 100;
96 | console.log(num1 + ' is ' + percent + '% of ' + num2);
97 | return percent;
98 | }
99 |
100 | percentOf(5, 10);
101 |
102 | function areaOfCircle(radius) {
103 | var area = Math.PI * squareNumber(radius);
104 | console.log('The area of circle with radius ' + radius + ' is ' + area);
105 | return area;
106 | }
107 |
108 | areaOfCircle(2);
109 |
110 | function doCrazyStuff(num) {
111 | var half = halfOf(num);
112 | var squared = squareNumber(half);
113 | var area = areaOfCircle(squared);
114 | var result = percentOf(squared, area);
115 | }
116 |
117 | doCrazyStuff(5);
118 |
119 | Challenge Questions: String Manipulation
122 |If you feel comfortable with the other exercises, it's time to expand your knowledge! These puzzles involve manipulating strings; to try them out, you'll need to use some of the built-in JavaScript methods for strings. Methods are pre-written functions that are built into the language.
123 | 124 |These questions are not as straightforward as the others. They challenge you to really think like a programmer. Really try to solve them before you peek at the answer.
125 | 126 |MixUp
127 |Create a function called mixUp
. It should take in two strings, and return the concatenation of the two strings (separated by a space) slicing out and swapping the first 2 characters of each. You can assume that the strings are at least 2 characters long. For example:
129 | mixUp('mix', pod'): 'pox mid' 130 | mixUp('dog', 'dinner'): 'dig donner' 131 |132 | 133 | 134 |
136 | //This function uses the slice() method. It extracts a part of a string and returns a new string
137 | function mixUp(string1, string2) {
138 | return string1.slice(0, 2) + string2.slice(2) + " " + string2.slice(0, 2) + string1.slice(2);
139 | }
140 |
141 | FixStart
144 |Create a function called fixStart
. It should take a single argument, a string, and return a version where all occurrences of its first character have been replaced with '*', except for the first character itself. You can assume that the string is at least one character long. For example:
146 | fixStart('babble'): 'ba**le' 147 | fixStart('turtle'): 'tur*le' 148 |149 | 150 | 151 |
153 | //This function uses a few new methods and regular expressions
154 | function fixStart(inputString) {
155 | var firstChar = inputString.charAt(0);
156 | return firstChar + inputString.slice(1).replace(new RegExp(firstChar, 'g'), '*');
157 | }
158 |
159 | Class 2
164 |What number is bigger?
165 |Write a function that compares two numbers and returns the larger one. Be sure to try it our with some different numbers. Bonus: add error messages if the numbers are equal or cannot be compared.
166 |Don't forget to test it!
167 | 168 | 169 |
171 | function greaterNum(num1, num2) {
172 | if (num1 === num2) {
173 | console.log ('Those numbers are equal');
174 | return num1;
175 | } else if (num1 > num2) {
176 | return num1;
177 | } else if (num2 > num1) {
178 | return num2;
179 | } else {
180 | console.log ('Those are simply incomparable!');
181 | return undefined;
182 | }
183 | }
184 |
185 | console.log (greaterNum(3, 3));
186 | console.log (greaterNum(7, -2));
187 | console.log (greaterNum(5, 9));
188 | console.log (greaterNum(5, 'dog'));
189 |
190 | Pluralize
193 |Write a function pluralize
that takes in two arguments, a number and a word, and returns the plural. For example:
195 | pluralize(5, 'cat'): '5 cats' 196 | pluralize(7, 'turtle'): '7 cats' 197 |198 |
Bonus: Make it handle a few collective nouns like "sheep" and "geese".
199 | 200 | 201 | 202 |
204 | function pluralize(number, noun) {
205 | if (number != 1 && noun != 'sheep' && noun != 'geese') {
206 | return number + ' ' + noun + 's';
207 | } else {
208 | return number + ' ' + noun;
209 | }
210 | }
211 | console.log('I have ' + pluralize(0, 'cat'));
212 | console.log('I have ' + pluralize(1, 'cat'));
213 | console.log('I have ' + pluralize(2, 'cat'));
214 | console.log('I have ' + pluralize(3, 'geese'));
215 |
216 | Even/Odd Counter
219 |Write a for loop that will iterate from 0 to 20. For each iteration, it will check if the current number is even or odd, and report that to the screen (e.g. "2 is even")
220 | 221 | 222 |
224 | for (var i = 0; i <= 20; i++) {
225 | if (i % 2 === 0) {
226 | console.log(i + ' is even');
227 | } else {
228 | console.log(i + ' is odd');
229 | }
230 | }
231 |
232 | Top Choice
235 |Create an array to hold your top choices (colors, presidents, whatever). For each choice, log to the screen a string like: "My #1 choice is blue." Bonus: Change it to log "My 1st choice, "My 2nd choice", "My 3rd choice", picking the right suffix for the number based on what it is.
236 | 237 | 238 |
240 | var choices = ['red', 'orange', 'pink', 'yellow'];
241 | for (var i = 0; i < choices.length; i++) {
242 | console.log('My #' + (i + 1) + ' choice is ' + choices[i]);
243 | }
244 |
245 | for (var i = 0; i < choices.length; i++) {
246 | var choiceNum = i + 1;
247 | var choiceNumSuffix;
248 | if (choiceNum == 1) {
249 | choiceNumSuffix = 'st';
250 | } else if (choiceNum == 2) {
251 | choiceNumSuffix = 'nd';
252 | } else if (choiceNum == 3) {
253 | choiceNumSuffix = 'rd';
254 | } else {
255 | choiceNumSuffix = 'th';
256 | }
257 | console.log('My ' + choiceNum + choiceNumSuffix + ' choice is ' + choices[i]);
258 | }
259 |
260 | Class 3
263 | 264 |The Reading List
265 |Keep track of which books you read and which books you want to read!
266 |-
267 |
- Create an array of objects, where each object describes a book and has properties for the title (a string), author (a string), and alreadyRead (a boolean indicating if you read it yet). 268 |
- Iterate through the array of books. For each book, log the book title and book author like so: "The Hobbit by J.R.R. Tolkien". 269 |
- Now use an if/else statement to change the output depending on whether you read it yet or not. If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolkien', and if not, log a string like 'You still need to read "The Lord of the Rings" by J.R.R. Tolkien.' 270 |
275 | var books = [
276 | {title: 'The Design of EveryDay Things',
277 | author: 'Don Norman',
278 | alreadyRead: false
279 | },
280 | {title: 'The Most Human Human',
281 | author: 'Brian Christian',
282 | alreadyRead: true
283 | }];
284 |
285 | for (var i = 0; i < books.length; i++) {
286 | var book = books[i];
287 | var bookInfo = book.title + '" by ' + book.author;
288 | if (book.alreadyRead) {
289 | console.log('You already read "' + bookInfo);
290 | } else {
291 | console.log('You still need to read "' + bookInfo);
292 | }
293 | }
294 |
295 | Logo Hijack
299 |-
300 |
- Open up www.google.com in Chrome or Firefox, and open up the console. 301 |
- Find the Google logo and store it in a variable. 302 |
- Modify the source of the logo IMG so that it's a Yahoo logo instead. (http://www.logostage.com/logos/yahoo.GIF) 303 |
- Find the Google search button and store it in a variable. 304 |
- Modify the text of the button so that it says "Yahooo!" instead. 305 |
310 | var img = document.getElementById('hplogo');
311 | img.width = 400;
312 | img.src = 'http://www.logostage.com/logos/yahoo.GIF';
313 | var button = document.getElementById('gbqfba');
314 | button.innerHTML = 'Yahoooo!';
315 |
316 | The Reading List Part II
319 |-
320 |
- Create a webpage with an
h1
of "My Book List".
321 | - Add a script tag to the bottom of the page, where all your JS will go. 322 |
- Copy the array of books from the previous exercise. 323 |
- Iterate through the array of books. For each book, create a
p
element with the book title and author and append it to the page.
324 | - Bonus: Use a
ul
andli
to display the books.
325 | - Bonus: add a property to each book with the URL of the book cover, and add an
img
element for each book on the page.
326 | - Bonus: Change the style of the book depending on whether you have read it or not. 327 |
332 | <!DOCTYPE html>
333 | <html>
334 | <head>
335 | <meta charset="utf-8"/>
336 | <title>Book List</title>
337 | </head>
338 | <body>
339 |
340 | <h1>My Book List</h1>
341 | <script>
342 | var books = [
343 | {title: 'The Design of EveryDay Things',
344 | author: 'Don Norman',
345 | alreadyRead: false
346 | },
347 | {title: 'The Most Human Human',
348 | author: 'Brian Christian',
349 | alreadyRead: true
350 | }];
351 |
352 | for (var i = 0; i < books.length; i++) {
353 | var bookP = document.createElement('p');
354 | var bookDescription = document.createTextNode(books[i].title + ' by ' + books[i].author);
355 | bookP.appendChild(bookDescription);
356 | document.body.appendChild(bookP);
357 | }
358 |
359 | // Or, with the bonuses:
360 | var books = [
361 | {title: 'The Design of EveryDay Things',
362 | img: 'http://ecx.images-amazon.com/images/I/41j2ODGkJDL._AA115_.jpg',
363 | author: 'Don Norman',
364 | alreadyRead: false
365 | },
366 | {title: 'The Most Human Human',
367 | img: 'http://ecx.images-amazon.com/images/I/41Z56GwEv9L._AA115_.jpg',
368 | author: 'Brian Christian',
369 | alreadyRead: true
370 | }];
371 |
372 | var bookList = document.createElement('ul');
373 | for (var i = 0; i < books.length; i++) {
374 | var bookItem = document.createElement('li');
375 | var bookImg = document.createElement('img');
376 | bookImg.src = books[i].img;
377 | bookItem.appendChild(bookImg);
378 | var bookDescription = document.createTextNode(books[i].title + ' by ' + books[i].author);
379 | bookItem.appendChild(bookDescription);
380 | if (books[i].alreadyRead) {
381 | bookItem.style.color = 'grey';
382 | }
383 | bookList.appendChild(bookItem);
384 | }
385 | document.body.appendChild(bookList);
386 | </script>
387 |
388 | </body>
389 | </html>
390 |
391 | Challenge Question: The Counter
394 |Write a function that takes a certain type of tag and counts the number of elements with that tag. The function should return "There a X tags of type y on the page.For example:
395 |396 | countTags('p'): 'There are 3 tags of type p on the page' 397 |398 | 399 | 400 |
402 | function countTags (tagType) {
403 | var tagArray = document.getElementsByTagName(tagType);
404 | console.log ('There are ' + tagArray.length + ' tags of type ' + tagType + ' on the page.');
405 | }
406 |
407 | Class 4
410 |Coming Soon!
411 | 412 |