Skip to content

Commit

Permalink
Merge pull request #225 from asarora72/as_git
Browse files Browse the repository at this point in the history
To start with I entered 4 basic functions and tested.
  • Loading branch information
l-white authored Aug 2, 2023
2 parents d03e876 + 28a7ce5 commit 7d5ff62
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
142 changes: 142 additions & 0 deletions Solutions/Amarjit_JS_fun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const identity = x => x;

const addb = (a,b) => a + b;

const subb = (a,b) => a - b;

const mulb = (a,b) => a * b;

const minb = (a,b) => {
if(a<b)
return a;
else
return b;
};

const maxb = (a,b) => {
if(a>b)
return a;
else
return b;
};

const add = (...nums) => {
let n=0;
for (let i=0;i<nums.length;i++) {
n+=nums[i];
}
return n;
}

const sub = (...nums) => {
let n=0;
for (let i=0;i<nums.length;i++) {
if(n===0)
n=nums[i];
else
n-=nums[i];
}
return n;
}

const mul = (...nums) => {
let n=1;
for (let i=0;i<nums.length;i++) {
n*=nums[i];
}
return n;
}

const min = (...nums) => {
let n=nums[0];
for (let i=1;i<nums.length;i++) {
if(nums[i]<n)
n=nums[i];
}
return n;
}

const max = (...nums) => {
let n=0;
for (let i=0;i<nums.length;i++) {
if(nums[i]>n)
n=nums[i];
}
return n;
}

const addRecurse = (...nums) => {
let sum = 0;
for (const num of nums) {
if(Array.isArray(num)) {
sum+=addRecurse(...num);
}
else if (typeof num==='number') {
sum+=num;
}
}
return sum;
}

const mulRecurse = (...nums) => {
let sum = 1;
for (const num of nums) {
if(Array.isArray(num)) {
sum*=mulRecurse(...num);
}
else if (typeof num==='number') {
sum*=num;
}
}
return sum;
}

const minRecurse = (...nums) => {
let min = Number.POSITIVE_INFINITY;
for (const num of nums) {
if(Array.isArray(num)) {
const nestedmin = minRecurse(...num);
if(nestedmin<min)
min=nestedmin;
}
else if (typeof num==='number') {
if(num<min)
min=num;
}
}
return min;
}

const maxRecurse = (...nums) => {
let max = Number.NEGATIVE_INFINITY;
for (const num of nums) {
if(Array.isArray(num)) {
const nestedmax = maxRecurse(...num);
if(nestedmax>max)
max=nestedmax;
}
else if (typeof num==='number') {
if(num>max)
max=num;
}
}
return max;
}

module.exports = {
identity,
addb,
subb,
mulb,
minb,
maxb,
add,
sub,
mul,
min,
max,
addRecurse,
mulRecurse,
minRecurse,
maxRecurse
};
18 changes: 18 additions & 0 deletions test/Amarjit_solutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mathModule = require('../Solutions/Amarjit_JS_fun');

console.log(mathModule.identity(5));
console.log(mathModule.addb(3, 7));
console.log(mathModule.subb(10, 5));
console.log(mathModule.mulb(2, 3));

console.log(mathModule.minb(5,6));
console.log(mathModule.maxb(3, 7));
console.log(mathModule.add(10,5,2,3));
console.log(mathModule.sub(10,2,3));
console.log(mathModule.mul(3, 7, 5));
console.log(mathModule.min(10,5,2,3));
console.log(mathModule.max(10,2,3,11));
console.log(mathModule.addRecurse(10,2,3,[2,3,[5]],11));
console.log(mathModule.mulRecurse(10,2,3,[2,3,[5]],11));
console.log(mathModule.minRecurse(10,2,3,[2,3,[5]],11));
console.log(mathModule.maxRecurse(10,2,3,[2,3,[5]],11));

0 comments on commit 7d5ff62

Please sign in to comment.