@ajaykrp/math-ops

1.0.2 • Public • Published

Math-OPS

A simple javascript function that can perform different arithmetic operations on variable arguments.

Features

  • Supports four basic operations: addition, subtraction, multiplication, and division.
  • Accepts any number of arguments for each operation.
  • Checks the validity of the arguments and the operation and throws an error if invalid.
  • Returns the final result of the calculation.

Installation

To use the math-ops function, you need to have Node.js installed on your system. Install it by typing following command

npm install --save @ajaykrp/math-ops

Usage

To use the math-ops function, you need to import it from the file where you saved it, such as index.js. Then, you can call the function with the operation and the arguments as parameters. For example:

// import the module
const calculate = require("@ajaykrp/math-ops");

// call the function with the operation and the arguments
let result = calculate("addition", 1, 2, 3, 4, 5);

// print the result
console.log(result); // 15

You can also use the function with other operations and arguments, such as:

// import the module
const calculate = require("@ajaykrp/math-ops");

// call the function with the operation and the arguments
let result1 = calculate("subtraction", 10, 5, 2); // 3
let result2 = calculate("multiplication", 2, 3, 4); // 24
let result3 = calculate("division", 20, 2, 2); // 5

// print the results
console.log(result1, result2, result3); // 3 24 5

If you pass an invalid operation or argument, the function will throw an error. For example:

// import the module
const calculate = require("@ajaykrp/math-ops");

// call the function with an invalid operation
let result = calculate("modulus", 10, 3);

// print the result
console.log(result); // Invalid operation. Please use one of the following: addition, subtraction, multiplication, or division.

In case of invalid numbers it will throw error as below example

// import the module
const calculate = require("@ajaykrp/math-ops");

// call the function with an invalid argument
let result = calculate("addition", 1, 2, "three");

// print the result
console.log(result); // Invalid argument: three

License

This project is licensed under the MIT License.

Readme

Keywords

Package Sidebar

Install

npm i @ajaykrp/math-ops

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

5.6 kB

Total Files

3

Last publish

Collaborators

  • ajaykrp