Welcome! This the homework to accompany the Girl Develop It JavaScript 101 class. 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!
1. The Fortune Teller
Why pay a fortune teller when you can just program your fortune yourself?
- Store the following into variables: number of children, partner's name, geographic location, job title.
- Output your fortune to the screen like so: "You will be a X in Y, and married to Z with N kids."
2. The Calculator
- 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." - 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.". - 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." - 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."- Bonus: Round the result so there are only two digits after the decimal.
- Write a function that will take one argument (a number) and perform the following operations, using the functions
you wrote earlier:
- Take half of the number and store the result.
- Square the result of #1 and store that result.
- Calculate the area of a circle with the result of #2 as the radius.
- Calculate what percentage that area is of the squared result (#3).
3. What number is bigger?
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.
Don't forget to test it!
4. Pluralize
Write a function pluralize
that takes in two arguments, a number and a word, and returns the plural. For example:
pluralize(5, 'cat'): '5 cats' pluralize(7, 'turtle'): '7 cats'
Bonus: Make it handle a few irregular plural nouns like "deer" and "geese".
5. Even/Odd Counter
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")
6. Top Choice
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.
Challenge Question!: MixUp
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:
mixUp('mix', pod'): 'pox mid' mixUp('dog', 'dinner'): 'dig donner'
Hint: 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.