Create a file named functions.js
.
- Define a
youRock
function that accepts a string argument of a name and returns a string using that name.
youRock('Samar') // 'You rock Samar!'
youRock('Sara') //'You rock Sara!'
- Define a
square
function that accepts a number argument and returns that number multipled by itself.
square(2) // 4
square(3) // 9
- Define a
cube
function that accepts a number argument and returns that number raised to the third power.
cube(2) // 8
cube(3) // 27
- Define a
toTheFourth
function that accepts a number argument and returns that number raised to the fourth power.
toTheFourth(2) // 16
toTheFourth(3) // 81
If you finish the Lab, try this challenge.
Write a function that will add, subtract, multiply or divide two numbers and return the answer
calculator(1, 2, "add") // should return 3
calculator(1, 2, "subtract") // should return -1
calculator(1, 2, "divide") // should return .5
calculator(1, 2, "multiply") // should return 2
calculator(1, 2, "something else") // should return "calculator can only add, subtract, divide, or multiply
calculator("cat", 2, "add") // should return "calculator only accepts numbers"
Write a function that accepts an argument of a number
If it is a multiple of 3, return “Fizz” instead of the number.
If it is a multiple of 5, return “Buzz” instead of the number.
If it is a multiple of both 3 and 5, return “FizzBuzz” instead of the number.
Otherwise, return the number
fizzBuzz(3) // Fizz
fizzBuzz(15) // FizzBuzz
fizzBuzz(7) // 7
Write a function that accepts an argument of a number
If the number contains 3 as a factor, output 'Pling'.
If the number contains 5 as a factor, output 'Plang'.
If the number contains 7 as a factor, output 'Plong'.
If the number does not contain 3, 5, or 7 as a factor, output the number as a string.
rainDrop(28) // Plong
rainDrop(1755) // PlingPlang
rainDrop(34) // 34