Skip to content

rukkiecodes/calculateSubtotal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

calculateSubTotal Function Documentation

Introduction

The calculateSubTotal function is a utility function designed to calculate the subtotal of items within an array, taking into consideration both the price and quantity of each item. The function accepts an array of items as its parameter and returns a promise that resolves to an object containing the calculated subtotal, total VAT, and final price.

Function Signature

const calculateSubTotal = arr => {
    // Function body
}

Parameters

  • arr: An array of items to be used for calculating the subtotal.

Function Logic

  1. Initialize the total variable to 0. This variable will be used to accumulate the subtotal as the calculation progresses.

  2. Loop through each item in the input array using a for loop.

    • For each item, the function performs the following calculations:

      • Extracts the price and quantity properties from the item.
      • Checks if price and quantity are defined and not null or undefined. If they are not, they are parsed as floating-point numbers. If they are null or undefined, they are set to 0.
    • Calculates the subtotal for the current item by multiplying the parsed price and quantity.

    • Adds the subtotal of the current item to the total variable to accumulate the overall subtotal.

  3. After processing all items in the array, the function creates a formattedResult object containing the following properties:

    • subTotal: The overall subtotal, formatted as a string with commas separating thousands (e.g., "1,000.00").
    • totalVAT: Initial value set to 0 (indicating no VAT).
    • finalPrice: The final calculated price, formatted as a string with commas (similar to subTotal).
  4. The function returns a promise that resolves to the formattedResult object.

Usage Example

import calculateSubTotal from './calculateSubTotal';

// Sample array of items
const items = [
    { price: 10.5, quantity: 3 },
    { price: 5, quantity: 2 },
    { price: 8, quantity: 4 },
];

// Calculate the subtotal using the calculateSubTotal function
calculateSubTotal(items)
    .then(result => {
        console.log(result.subTotal); // Display the subtotal
        console.log(result.totalVAT); // Display the total VAT
        console.log(result.finalPrice); // Display the final price
    });

GitHub Repository

Notes

  • This function assumes that the input data is well-structured and that the properties price and quantity exist for each item in the array.

  • The function can be integrated into a larger application to handle calculations involving items and their prices.


This updated documentation now includes a link to the GitHub repository for reference.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published