PREV: Type Ninjery
NEXT: Types and Values Ninjery
A series of exercises with unique tasks for playing around with the basic Elm types we've come across so far, and getting more familiar with conditional logic.
-
Function Naming: Choose descriptive and meaningful names for your functions. Avoid generic names like
func
ormyFunction
. The name should reflect the task performed by the function. -
Conditional Structures: Begin by implementing the function using
if-else
statements. Afterward, consider if the same functionality could be achieved more elegantly usingcase
statements. -
Reflection and Critical Thinking:
- If you implement both
if-else
andcase
versions for an exercise, reflect on which version you find more intuitive or readable, and explain why in a comment. - If only one method seems appropriate, explain why the other method was less suitable or not feasible in your case.
- If you implement both
-
Error Handling: Ensure your functions gracefully handle edge cases and invalid inputs. Where applicable, use humor or creativity in your error messages to make the exercises more engaging.
-
Testing: Test your functions with various inputs to ensure they behave as expected. Pay particular attention to edge cases and boundary conditions.
- Write Elm functions for each of the provided exercises, adhering to the task descriptions.
- Include comments in your code to explain your approach, especially for your choice of function names and conditional structures.
- Submit both versions of the functions (using
if-else
andcase
statements) where applicable, along with your reflections.
Create a function that takes an integer score (between 0 and 100) and returns the corresponding letter grade. The function should adhere to the following grading scale:
Score | Grade |
---|---|
90 - 100 | A |
80 - 89 | B |
70 - 79 | C |
60 - 69 | D |
0 - 59 | F |
If the score is outside the range of 0 to 100, the function should indicate that the score is invalid.
- If the function receives the score
49
, it should return"F"
. - If it receives
101
, it should return a message indicating that the score is invalid, such as"Invalid Score"
.
Create a function that accepts a single character and categorizes it. The function should differentiate between characters and assign them to categories as follows:
- "Vowel": for vowel characters (both uppercase and lowercase).
- "Consonant": for consonant characters (both uppercase and lowercase).
- "Digit": for numeric characters.
- "Unknown": for all other types of characters.
- If the function receives the character
'a'
, it should return "Vowel". - If the function receives the character
'A'
, it should also return "Vowel". - If it receives
'z'
, the function should return "Consonant". - For a digit like
'3'
, the function should return "Digit". - If the function receives a symbol like
'@'
, it should return "Unknown".
Create a function that takes an integer representing a person's age and classifies it into age groups. The function should categorize ages as follows:
- "Child": for ages 0 to 12.
- "Teenager": for ages 13 to 19.
- "Adult": for ages 20 to 64.
- "Senior": for ages 65 to 122.
- "Guinness World Records, let's goooooooo!": for ages 123 to 968.
- "Methuselah?!": for ages 969 and above.
If the age is negative, the function should respond with "Invalid Age".
- If the function receives the age
10
, it should return "Child". - For an age like
15
, the function should return "Teenager". - Receiving age
30
, it should return "Adult". - For age
70
, the function should return "Senior". - If the function receives an age like
150
, it should amusingly return "Guinness World Records, let's goooooooo!". - For an age like
1000
, it should whimsically return "Methuselah?!". - Receiving a negative number like
-5
, it should return "Invalid Age".
Create a function that takes an integer (from 1 to 12) and returns the corresponding gift for that day according to the song "The Twelve Days of Christmas". The function should handle numbers outside this range by indicating that the day is invalid.
- If the function receives
1
, it should return "A Partridge in a Pear Tree". - For the input
5
, it should return "Five Golden Rings". - Receiving
8
, the function should return "Eight Maids a Milking". - If the function receives a number like
13
, it should return "Invalid Day".
Create a function that takes two inputs: a month (as a string) and a day (as an integer). The function should check if the date is valid, considering the standard number of days in each month and excluding leap years. It should return a string indicating whether the date is valid or not, and if invalid, provide a reason for the invalidation.
- If the function receives
"February"
and28
, it should return "Valid Date". - For inputs
"April"
and30
, it should return "Valid Date". - Receiving
"June"
and31
, the function should return "Invalid Date: June has only 30 days". - If the function receives
"November"
and31
, it should return "Invalid Date: November has only 30 days". - For an invalid month like
"Hogwarts"
with any day, the function should return "Invalid Date: 'Hogwarts' is not a valid month".
Create a function that simulates a traffic light. The function should take a string representing the light color, capitalized (e.g., "Red", "Yellow", "Green"), and return the corresponding action. For any invalid color inputs, the function should provide a humorous and appropriate response.
You can expect the input colors to be provided in capitalized form, such as "Red", "Yellow", and "Green".
- If the function receives
"Red"
, it should return "Stop". - For the input
"Yellow"
, it should return "Prepare to stop". - Receiving
"Green"
, the function should return "Go". - If the function receives an unusual color like
"Purple"
, it could whimsically return "Purple? Are we on Mars?".
Create a function that takes an integer (1-12) representing a month number and returns the name of the corresponding month. If the number is outside the range of 1 to 12, the function should return a humorous comment indicating the invalidity.
- If the function receives
1
, it should return "January". - For the input
5
, it should return "May". - Receiving
12
, the function should return "December". - If the function receives a number like
13
, it could whimsically return "Yeah, right, there's a 13th month!".
Create a function that takes an integer representing an hour of the day in 24-hour format (ranging from 0 to 23) and converts it to a 12-hour format with AM/PM. The function should return a tuple consisting of the converted hour (as an integer) and either "AM" or "PM" (as a string). For hours outside the 0 to 23 range, the function should return (-1, "Extra time? This isn't football, hun!")
as a humorous response.
- If the function receives
0
, it should return (12, "AM"). - For the input
13
, it should return (1, "PM"). - Receiving
15
, the function should return (3, "PM"). - If the function receives
23
, it should return (11, "PM"). - For an hour like
25
, it should return (-1, "Extra time? This isn't football, hun!").
Create a function for a variant of the FizzBuzz game. The function should accept an integer and apply the following rules:
- Return "Fizz" if the number is divisible by 3.
- Return "Buzz" if it's divisible by 5.
- Return "FizzBuzz" if it's divisible by both 3 and 5.
- Otherwise, return the number itself as a string. Incorporate a humorous twist for special cases, such as extremely high numbers or negative numbers.
- If the function receives
3
, it should return "Fizz". - For the input
10
, it should return "Buzz". - Receiving
15
, the function should return "FizzBuzz". - If the function receives a number like
101
, it could return "101 - Just another number in the universe!". - For a negative number like
-5
, it might whimsically return "Buzzing into the negatives, are we?".
Create a function that identifies the typical mood associated with each day of the week. The function should take a string representing a day of the week, capitalized (e.g., "Monday", "Tuesday"), and return a phrase capturing the general sentiment for that day. Here's how to categorize each day:
- "Monday": Return "Back to the grind!".
- "Tuesday", "Wednesday", "Thursday": Return "Just another day in the grind".
- "Friday": Return "TGIF!".
- "Saturday", "Sunday": Return "It's the weekend, baby!".
For any input that is not a recognized capitalized day of the week, the function should provide a light-hearted response, such as "Is that a public holiday?".
You can expect the input days to be provided in capitalized form, such as "Monday", "Tuesday", etc.
- If the function receives
"Monday"
, it should return "Back to the grind!". - For the input
"Friday"
, it should return "TGIF!". - Receiving
"Sunday"
, the function should return "It's the weekend, baby!". - If the function receives an unusual day like
"Moonday"
, it could whimsically return "Is that a public holiday?".
PREV: Type Ninjery
NEXT: Types and Values Ninjery
For the age problem, a funny response to invalid ages could be "Hold on, Benjamin Burton!"