JSFiddle - React, Tailwind, and code Playground
JavaScript
var Mcd = function(numbers){
if(numbers.length >= 2){
this.numbers = numbers;
this.mcdNumber = this.calculateMcd(0, numbers.length - 1);
} else {
return 'error';
}
}
Mcd.prototype.calculateMcd = function(start, end){
if( start + 1 == end ){
return this.getMcd( this.numbers[start], this.numbers[end] );
} else {
return this.getMcd(this.numbers[start], this.calculateMcd(start+1,end));
}
}
Mcd.prototype.getMcd = function(a, b){
if( b == 0 )
return a;
else
return this.getMcd( b, a%b );
}
Mcd.prototype.getResult = function(){
return this.mcdNumber;
};
var Mcm = function(numbers){
if(numbers.length >= 2){
this.numbers = numbers;
this.mcnNumber = this.calculateMcm(0, numbers.length - 1);
} else {
return 'error';
}
}
Mcm.prototype.calculateMcm = function(start, end){
if( start + 1 == end ){
return this.getMcm( this.numbers[start], this.numbers[end] );
} else {
return this.getMcm(this.numbers[start], this.calculateMcm(start+1,end));
}
}
Mcm.prototype.getMcm = function(a, b){
var mcd = new Mcd( [a, b] );
return (a*b)/mcd.getResult();
}
Mcm.prototype.getResult = function(){
return this.mcnNumber;
};
var mcd = new Mcd([36, 12, 96]);
console.log(mcd.getResult());
var mcm = new Mcm([324,16,7,5]);
console.log(mcm.getResult());