JavaScript Coding Test Course, Helping the Underprivileged

Problem Description

We are trying to raise donations to help the less fortunate. Each donor simply needs to input the amount they can donate. The amount entered by the donor is stored in a list, and a function to calculate the total amount donated is to be implemented. Additionally, the maximum amount a donor can donate is limited to 100,000 won.

Input

The number of donors n (1 <= n <= 1000) and each donor’s donation amount are provided.

Output

Print the total sum of all donations.

Example Input

    5
    10000
    25000
    50000
    120000
    30000
    

Example Output

    95000
    

Approach to Problem Solving

This problem requires a simple algorithm to process the given input values and calculate the total. We can approach it by storing the donation amounts in an array and summing all elements in the array to derive the result. The following are the steps to solve this problem.

Step 1: Taking Input

To take input, we will receive the number of donors (n) and each donor’s donation amount from the user. These input values will be stored in an array. Donations exceeding 100,000 won will be ignored, so a logic to check this is needed.

Step 2: Storing Donation Amounts in an Array

Only valid donation amounts will be added to the array, and a variable will be set to calculate the total donation amount.

Step 3: Calculating and Printing the Total

Calculate the sum of donation amounts stored in the array and print the result.

JavaScript Code Implementation

Now let’s implement the entire logic in JavaScript code. Below is the code based on the logic described above:


function calculateDonation() {
    const n = parseInt(prompt("Please enter the number of donors: "));
    let donations = [];
    let totalDonation = 0;

    for (let i = 0; i < n; i++) {
        let donation = parseInt(prompt(`Please enter the ${i + 1}th donation amount: `));

        // Add to the array only if the donation amount is 100,000 won or less
        if (donation <= 100000) {
            donations.push(donation);
            totalDonation += donation;
        } else {
            console.log("The donation amount must be 100,000 won or less.");
        }
    }

    console.log(`Total donations: ${totalDonation} won`);
}

calculateDonation();
    

Code Explanation

Analyzing the code, the calculateDonation function is defined, where the user first inputs the number of donors. Then, using a for loop, each donation amount is inputted, and a conditional statement adds it to the array only if it is 100,000 won or less, calculating the sum at the same time. If the donation amount exceeds this, a warning message is printed. Finally, the total donation amount is displayed.

Conclusion

In this tutorial, we implemented a simple program to manage donation data using JavaScript. This code allows easy calculation of total donations and includes logic to check the number of donors and the validity of donation amounts. Through this experience of solving simple problems, we can gain confidence in solving algorithmic challenges.

Additional Practice Problems

To further practice problem solving, try tackling the following additional problems:

  1. Add a feature to sort the donation amounts in ascending order.
  2. Calculate the average donation amount of the donors.
  3. Implement a feature to automatically add an additional 10% donation when the number of donors exceeds 10.

Final Remarks

Programming practice requires a deep understanding that goes beyond just solving problems. Based on the experiences and knowledge gained through this problem-solving process, aim to create your own programs or tackle more complex algorithmic challenges with confidence.