Skip to Content
Lab 1

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

  1. Create a function called formatFullName that takes two parameters: firstName and lastName. The function should return the full name in the format lastName, firstName.
  2. Format the firstName and lastName so that the first letter of each is always capatilized.
  3. 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

  1. Write a function called calculateTotalCost that takes three parameters: price, quantity, and taxRate.
  2. The function should calculate the total cost of items, including tax. Use the formula: totalCost = (price * quantity) * (1 + taxRate).
  3. Ensure that price, quantity, and taxRate are all valid numbers. If any are invalid, return a string: “Invalid input.”

Task 3: Functions with Conditional Logic

  1. Write a function called checkEligibility that takes two parameters: age and isEmployed. 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.
  2. Return an appropriate string message for each scenario.

Task 4: Refactoring for Reusability

  1. Refactor the calculateTotalCost function from Task 2 to include an optional discount parameter. If the discount is provided, the function should subtract the discount from the total cost before applying tax.
  2. If no discount is provided, calculate the total cost as before.

Submission Instructions

  1. Complete all the tasks outlined above.
  2. Ensure that each task works as expected by testing it in your local development environment.
  3. 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:

  1. How did you approach creating more flexible functions with parameters?
  2. What challenges did you face while incorporating conditional logic in your functions?
  3. How does refactoring improve the readability and maintainability of your code?

Additional Resources