Exercise: Free Coffee Cups


For each of the 6 coffee cups I buy, I get a 7th cup free. In total, I get 7 cups.

Knowing that I paid for cups cups, compute and print the total number of cups I would get (number of cups I paid for + number of cups I got for free).


Example:

For example, if cups was 12, the answer would be 14.

Why? Because I paid for 12 cups and I got 2 for free, which is a total of 14 cups.

Introduction

In this lesson, we will explore a common promotional offer where for every 6 coffee cups you buy, you get an additional cup for free. This type of problem is significant in programming as it involves basic arithmetic operations and conditional logic, which are fundamental concepts in many real-world applications. Such scenarios are particularly useful in retail and e-commerce platforms where promotions and discounts are frequently applied.

Understanding the Basics

Before diving into the solution, let's understand the basic concept. The promotion states that for every 6 cups you buy, you get 1 cup free. Therefore, if you buy 6 cups, you get 7 cups in total. If you buy 12 cups, you get 14 cups in total, and so on. The key here is to determine how many free cups you get based on the number of cups you paid for.

Main Concepts

The main concept here is to calculate the number of free cups based on the number of cups you paid for. This can be done using simple division and multiplication operations. The formula to calculate the total number of cups is:

totalCups = paidCups + Math.floor(paidCups / 6);

Here, Math.floor(paidCups / 6) calculates the number of free cups you get.

Examples and Use Cases

Let's look at a few examples to understand this better:

Common Pitfalls and Best Practices

One common mistake is to forget to use the Math.floor function, which ensures that we only count complete sets of 6 cups. Another pitfall is not handling edge cases, such as when the number of cups is less than 6. Best practices include writing clean and readable code, using meaningful variable names, and adding comments to explain the logic.

Advanced Techniques

For more advanced scenarios, you could consider cases where the promotion changes, such as getting a free cup for every 5 cups bought. The logic would remain similar, but the divisor would change. Additionally, you could implement this logic in a function that takes the number of cups and the promotion details as parameters.

Code Implementation

Here is the JavaScript code to solve this problem:

/**
 * Function to calculate the total number of coffee cups including free cups.
 * @param {number} paidCups - The number of cups paid for.
 * @returns {number} - The total number of cups including free cups.
 */
function totalCoffeeCups(paidCups) {
  // Calculate the number of free cups
  const freeCups = Math.floor(paidCups / 6);
  // Calculate the total number of cups
  const totalCups = paidCups + freeCups;
  return totalCups;
}

// Example usage:
const paidCups = 12;
console.log(totalCoffeeCups(paidCups)); // Output: 14

Debugging and Testing

To debug and test this function, you can use various test cases to ensure it works correctly:

Use console logs or a testing framework like Jest to automate these tests.

Thinking and Problem-Solving Tips

When approaching problems like this, break down the problem into smaller parts. First, understand the promotion rule, then translate it into a mathematical formula. Write pseudocode to outline your logic before implementing it in code. Practice similar problems to improve your problem-solving skills.

Conclusion

In this lesson, we covered how to calculate the total number of coffee cups you get based on a promotional offer. We discussed the basic concepts, provided examples, and implemented the solution in JavaScript. Understanding such problems helps in developing logical thinking and problem-solving skills, which are crucial in programming.

Additional Resources