adv_calculator
v1.1.8
Published
Scientific Calculator
Downloads
356
Maintainers
Readme
Arithmetic Functions Module
A Node.js module providing basic arithmetic functions: addition, subtraction, multiplication, and division. This module is designed for educational purposes to demonstrate how to write and test JavaScript functions.
Table of Contents
Overview
This module includes the following functions:
Arithmetic Functions
add(a, b)
: Returns the sum ofa
andb
.subtract(a, b)
: Returns the difference whenb
is subtracted froma
.multiply(a, b)
: Returns the product ofa
andb
.divide(a, b)
: Returns the quotient whena
is divided byb
. Throws an error ifb
is zero.exponential(base, exponent)
: Returns the result ofbase
raised to the power ofexponent
.modulus(a, b)
: Returns the remainder whena
is divided byb
.squareRoot(a)
: Returns the square root ofa
.absolute(a)
: Returns the absolute value ofa
.factorial(n)
: Returns the factorial ofn
.gcd(a, b)
: Returns the greatest common divisor ofa
andb
.lcm(a, b)
: Returns the lowest common multiple ofa
andb
.
Bitwise Functions
bitwiseAnd(a, b)
: Performs a bitwise AND operation ona
andb
.bitwiseOr(a, b)
: Performs a bitwise OR operation ona
andb
.bitwiseXor(a, b)
: Performs a bitwise XOR operation ona
andb
.bitwiseNot(a)
: Performs a bitwise NOT operation ona
.leftShift(a, b)
: Performs a left bitwise shift ona
byb
bits.rightShift(a, b)
: Performs a right bitwise shift ona
byb
bits.zeroFillRightShift(a, b)
: Performs a zero-fill right shift ona
byb
bits.
Installation
To use this module in your project, follow these steps:
Clone the repository:
git clone https://github.com/TyRoopam9599/adv_calculator.git
Navigate to the project directory:
cd adv_calculator
Install dependencies:
This project uses Mocha for testing. Install it along with other dependencies using:
npm install
Usage
You can use the functions by requiring the module in your Node.js application. Here’s how you can do it:
const {
add,
subtract,
multiply,
divide,
exponential,
modulus,
bitwiseAnd,
bitwiseOr,
bitwiseXor,
bitwiseNot,
leftShift,
rightShift,
zeroFillRightShift,
squareRoot,
absolute,
factorial,
gcd,
lcm
} = require('./index');
console.log(add(2, 3)); // Output: 5
console.log(subtract(5, 2)); // Output: 3
console.log(multiply(2, 3)); // Output: 6
console.log(divide(6, 2)); // Output: 3
console.log(modulus(5, 2)); // Output: 1
console.log(exponential(2, 3)); // Output: 8
console.log(squareRoot(9)); // Output: 3
console.log(absolute(-5)); // Output: 5
console.log(factorial(5)); // Output: 120
console.log(gcd(5,2)) ; // Output: 1
condole.log(lcm(5,2)) ; // Output: 10
console.log(bitwiseAnd(5, 3)); // Output: 1
console.log(bitwiseOr(5, 3)); // Output: 7
console.log(bitwiseXor(5, 3)); // Output: 6
console.log(bitwiseNot(5)); // Output: -6
console.log(leftShift(5, 1)); // Output: 10
console.log(rightShift(5, 1)); // Output: 2
console.log(zeroFillRightShift(5, 1)); // Output: 2
// Handle division by zero
try {
console.log(divide(6, 0));
} catch (error) {
console.error(error.message); // Output: Cannot divide by zero
}