Exercise - Functions
Functions are important building blocks in any language. They help ensure that your code follows the DRY principle and that you follow a design principle called Separation of Concerns. Your functions should “do one thing and do it well” (McIlroy). These exercises will help strengthen your function know-how.
-
Write a function that accepts two numbers as parameters, and returns the sum.
-
Write a function that accepts three numbers as parameters, and returns the largest of those numbers.
-
Write a function that accepts one number as a parameter, and returns whether that number is even or odd. (Return the string "even" or "odd");
Optional Challenges
- Write a function that accepts a string as a parameter. If the length of the string is less than or equal to twenty characters long, return the string concatenated with itself (string + string). If the string is more than twenty characters long, return the first half of the string. You will need to use a string method or two:
-slice()
-concat() (optional)
- Write a function that accepts a number ‘n’ as a parameter. Then print the first ‘n’ Fibonacci numbers and return their sum.
The first numbers are:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
So if n
were 6
, the sum of 1+1+2+3+5+8 would be 20
Don't hardcode the sequence.
- Write a function that accepts a string as a parameter. Return the most frequently occuring letter in that string. ( White spaces count as a letter )