Basic JavaScript Tasks

Functions and Control Flow

Open the console to view the results

        

What number is bigger?

Write a function that compares two numbers and returns the larger one. Be sure to try it out with some different numbers. Bonus: add error messages if the numbers are equal or cannot be compared. Don’t forget to test it!
Input two numbers



        

The Fortune Teller: With Functions!

Let’s turn one of the Class 1 Exercises into a function. Write a function named tellFortune that: takes 4 arguments: number of children, partner’s name, geographic location, job title. outputs your fortune to the screen like so: “You will be a X in Y, and married to Z with N kids.” Call that function 3 times with 3 different values for the arguments.



        

The Calculator

1. 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.” 2. 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.”. 3. 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.” 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: 1. Take half of the number and store the result. 2. Square the result of #1 and store that result. 3. Calculate the area of a circle with the result of #2 as the radius. 4. Calculate what percentage that area is of the squared result (#3).
Input a number ( an argument)