Skip to content

Commit

Permalink
0.0.2v
Browse files Browse the repository at this point in the history
  • Loading branch information
kry008 committed Apr 4, 2023
1 parent 3c9fa0c commit 63eaa06
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# calc-new
![GitHub pull requests](https://img.shields.io/github/issues-pr-raw/kry008/calc-new) ![GitHub package.json version](https://img.shields.io/github/package-json/v/kry008/calc-new) ![GitHub Repo stars](https://img.shields.io/github/stars/kry008/calc-new?style=social)
A small library with many math operations
162 changes: 162 additions & 0 deletions class/basicMath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
class BasicMath
{
addition(a, b)
{
return {a: a, b: b, result: a + b};
}
addition(a, b, c)
{
return {a: a, b: b, c: c, result: a + b + c};
}
addition(a)
{
var sum = 0;
for (var i = 0; i < a.length; i++)
{
sum += a[i];
}
return {a: a, result: sum};
}
subtraction(a, b)
{
return {a: a, b: b, result: a - b};
}
subtraction(a, b, c)
{
return {a: a, b: b, c: c, result: a - b - c};
}
subtraction(a)
{
var sum = 0;
for (var i = 0; i < a.length; i++)
{
sum -= a[i];
}
return {a: a, result: sum};
}
multiplication(a, b)
{
return {a: a, b: b, result: a * b};
}
multiplication(a, b, c)
{
return {a: a, b: b, c: c, result: a * b * c};
}
multiplication(a)
{
var sum = 1;
for (var i = 0; i < a.length; i++)
{
sum *= a[i];
}
return {a: a, result: sum};
}
division(a, b)
{
if (b == 0)
{
return {a: a, b: b, result: "Cannot divide by zero"};
}
return {a: a, b: b, result: a / b};
}
division(a, b, c)
{
if (b == 0 || c == 0)
{
return {a: a, b: b, c: c, result: "Cannot divide by zero"};
}
return {a: a, b: b, c: c, result: a / b / c};
}
division(a)
{
var sum = 1;
for (var i = 0; i < a.length; i++)
{
//check for divide by zero
if (a[i] == 0)
{
return {a: a, result: "Cannot divide by zero"};
}
sum /= a[i];
}
return {a: a, result: sum};
}
moduo(a, b)
{
return {a: a, b: b, result: a % b};
}
power(a, b)
{
if(b == 0)
{
return {a: a, b: b, result: 1};
}
if(b < 0)
{
// Example: 5^3 = 1 ÷ 5 ÷ 5 ÷ 5 = 0,008
let sum = 1;
for (var i = 0; i < b; i++)
{
sum /= a;
}
return {a: a, b: b, result: sum};
}
if(b > 0)
{
let sum = 1;
for (var i = 0; i < b; i++)
{
sum *= a;
}
return {a: a, b: b, result: sum};
}
}
absolute(a)
{
if(a < 0)
{
return {a: a, result: a * -1};
}
return {a: a, result: a};
}
squareRoot(a)
{
return {a: a, result: a*a}
}
logarithm(a, b)
{
if(a == 0)
{
return {a: a, b: b, result: "Cannot divide by zero"};
}
if(b == 0)
{
return {a: a, b: b, result: "Cannot divide by zero"};
}
if(a == 1)
{
return {a: a, b: b, result: "Cannot divide by zero"};
}
if(a == -1)
{
return {a: a, b: b, result: "Cannot divide by zero"};
}
if(a == b)
{
return {a: a, b: b, result: 1};
}
if(a == -b)
{
return {a: a, b: b, result: 1};
}
let sum = 1;
for (var i = 0; i < b; i++)
{
sum *= a;
}
return {a: a, b: b, result: sum};
}

}

module.exports = BasicMath;
6 changes: 6 additions & 0 deletions class/functionsMath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class functionsMath
{

}

module.exports = functionsMath;
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let basicMath = require('./class/basicMath.js');
let functionsMath = require('./class/functionsMath.js');
class allMath {
constructor() {
this.basicMath = new basicMath();
this.functionsMath = new functionsMath();
}

}

module.exports = {
basicMath: basicMath,
functionsMath: functionsMath,
allMath: allMath
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "calc-new",
"version": "0.0.1",
"version": "0.0.2",
"description": "A small library with many math operations",
"main": "index.js",
"scripts": {
Expand Down
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let math_c = require('./index.js');
let math_basicMath = new math_c.basicMath();

console.log(math_basicMath.addition(1, 2));
console.log(math_basicMath.addition(1, 2).result);
console.log(math_basicMath.addition(1, 2, 3));
console.log(math_basicMath.addition(1, 2, 3).result);
console.log(math_basicMath.addition([1, 2, 3]));
console.log(math_basicMath.addition([1, 2, 3]).result);
console.log(math_basicMath.subtraction(1, 2));
console.log(math_basicMath.subtraction(1, 2).result);
console.log(math_basicMath.subtraction(1, 2, 3));
console.log(math_basicMath.subtraction(1, 2, 3).result);
console.log(math_basicMath.subtraction([1, 2, 3]));
console.log(math_basicMath.subtraction([1, 2, 3]).result);
console.log(math_basicMath.multiplication(1, 2));
console.log(math_basicMath.multiplication(1, 2).result);
console.log(math_basicMath.multiplication(1, 2, 3));
console.log(math_basicMath.multiplication(1, 2, 3).result);
console.log(math_basicMath.multiplication([1, 2, 3]));
console.log(math_basicMath.multiplication([1, 2, 3]).result);
console.log(math_basicMath.division(1, 2));
console.log(math_basicMath.division(1, 2).result);
console.log(math_basicMath.division(1, 2, 3));
console.log(math_basicMath.division(1, 2, 3).result);
console.log(math_basicMath.division([1, 2, 3]));
console.log(math_basicMath.division([1, 2, 3]).result);
console.log(math_basicMath.division(1, 0));
console.log(math_basicMath.division(1, 0).result);
console.log(math_basicMath.division(1, 0, 3));
console.log(math_basicMath.division(1, 0, 3).result);
console.log(math_basicMath.division([1, 2, 0]));
console.log(math_basicMath.division([1, 2, 0]).result);

0 comments on commit 63eaa06

Please sign in to comment.