Variables and Functions
Scenario
You are tasked with writing JavaScript code for a data processing tool. This tool will handle user input, perform mathematical calculations, manipulate strings, and provide dynamic feedback based on user actions. As the team’s JavaScript expert, you need to write efficient and reusable functions to ensure your code is scalable.
In this activity, you will solve more coding challenges that involve creating and manipulating variables, using functions with multiple parameters, and handling conditional logic. The tasks will focus on making your functions more flexible and reusable, a skill that will be crucial for larger projects.
Learning Objectives
By the end of this activity, you will have demonstrated your ability to:
- Manipulate variables to perform mathematical operations and string manipulation.
- Write and call functions that use parameters and return values.
- Use conditional logic inside functions to create more dynamic behavior.
- Refactor code to improve readability and reusability.
Instructions
Task 1: Flexible String Manipulation with Functions
- Create a function called
formatFullName
that takes two parameters:firstName
andlastName
. The function should return the full name in the formatlastName, firstName
. - Format the
firstName
andlastName
so that the first letter of each is always capatilized. - Add a condition that checks if either the first or last name is missing. If so, return a string saying “Invalid name input.”
Task 2: Mathematical Operations with Multiple Parameters
- Write a function called
calculateTotalCost
that takes three parameters:price
,quantity
, andtaxRate
. - The function should calculate the total cost of items, including tax. Use the formula:
totalCost = (price * quantity) * (1 + taxRate)
. - Ensure that
price
,quantity
, andtaxRate
are all valid numbers. If any are invalid, return a string: “Invalid input.”
Task 3: Functions with Conditional Logic
- Write a function called
checkEligibility
that takes two parameters:age
andisEmployed
. The function should check if a person is eligible for a program based on the following rules:- If the person is over 18 and employed, they are eligible.
- If the person is over 18 but unemployed, they are conditionally eligible.
- If the person is 18 or younger, they are not eligible.
- Return an appropriate string message for each scenario.
Task 4: Refactoring for Reusability
- Refactor the
calculateTotalCost
function from Task 2 to include an optionaldiscount
parameter. If thediscount
is provided, the function should subtract the discount from the total cost before applying tax. - If no
discount
is provided, calculate the total cost as before.
Submission Instructions
- Complete all the tasks outlined above.
- Ensure that each task works as expected by testing it in your local development environment.
- Once complete, submit your code by sharing the link to a GitHub repository containing the file via Canvas.
Reflection Questions
After completing this activity, take a few minutes to reflect on the following questions:
- How did you approach creating more flexible functions with parameters?
- What challenges did you face while incorporating conditional logic in your functions?
- How does refactoring improve the readability and maintainability of your code?