Exercise: Create Function with Parameters in JavaScript


We've written this code:

console.log("My name is Andy");
console.log("I code in Python");
console.log("I have a dog");

console.log("My name is Mike");
console.log("I code in Java");
console.log("I have a cat");

console.log("My name is Sarah");
console.log("I code in JavaScript");
console.log("I have a parrot");

Write the same code in a better way by creating and calling a function with parameters printMessages()

Understanding the Problem

The core challenge here is to avoid repetitive code by using a function that can handle different inputs. This is a common practice in programming to make the code more modular, readable, and maintainable.

Significance: Functions with parameters allow us to reuse code efficiently. This is particularly useful in scenarios where the same logic needs to be applied to different data sets.

Potential Pitfalls: A common misconception might be to create multiple functions for each set of messages, which would still be repetitive. The goal is to create a single function that can handle all variations.

Approach

1. **Naive Solution**: The naive approach is to write the console.log statements repeatedly for each set of messages. This is not optimal as it leads to code duplication.

2. **Optimized Solution**: Create a function printMessages() that takes parameters for the name, programming language, and pet. This function will then print the messages using these parameters.

Algorithm

1. Define the function printMessages() with three parameters: name, language, and pet.

2. Inside the function, use console.log() to print the messages, incorporating the parameters.

3. Call the function with different sets of arguments to print the desired messages.

Code Implementation

// Define the function with parameters
function printMessages(name, language, pet) {
    // Print the messages using the parameters
    console.log("My name is " + name);
    console.log("I code in " + language);
    console.log("I have a " + pet);
}

// Call the function with different sets of arguments
printMessages("Andy", "Python", "dog");
printMessages("Mike", "Java", "cat");
printMessages("Sarah", "JavaScript", "parrot");

Complexity Analysis

The time complexity of this solution is O(1) because the function performs a constant number of operations regardless of the input size. The space complexity is also O(1) as we are not using any additional data structures that grow with input size.

Edge Cases

Potential edge cases include passing empty strings or null values as parameters. The function should handle these gracefully, possibly by adding default values or validation checks.

Example Edge Case:

printMessages("", "", ""); // Should print messages with empty values

Testing

To test the solution comprehensively, we should include a variety of test cases:

  • Normal cases with valid strings
  • Edge cases with empty strings or null values
  • Cases with special characters in the strings

Example Test Cases:

printMessages("Andy", "Python", "dog");
printMessages("Mike", "Java", "cat");
printMessages("Sarah", "JavaScript", "parrot");
printMessages("", "", "");
printMessages("John", "C++", "hamster");

Thinking and Problem-Solving Tips

When approaching such problems, always look for patterns and repetitions in the code. Functions are a powerful tool to encapsulate repetitive logic and make your code more modular. Practice by identifying repetitive code in your projects and refactoring it into functions.

Conclusion

In this exercise, we learned how to refactor repetitive code by using a function with parameters. This approach makes the code more readable, maintainable, and efficient. Understanding and applying such techniques is crucial for writing clean and efficient code.

Additional Resources