JSFiddle - React, Tailwind, and code Playground

by Brondem Brondem

JavaScript

function valido( num ) {
    return typeof num === 'number' && num !== 0;
}

// MCD de dos numeros 
function MCD2( a, b ) {
    if( valido(a) && valido(b) ) {
        if( a < 0 ) a = -a;
        if( b < 0 ) b = -b;
        var r;
        while( r = a % b ) {
            a = b;
            b = r;
        }
        return b;
    } else return false;
}

// mcm de 2 numeros
function mcm2( a, b ) {
    var mcd = MCD2( a, b );
    return mcd ? a*b/mcd : false;
}

function plegar( func, args ) { 
    var n = args.length;
    var array;
    if( n < 1 ) {
        return false;
    } else if( n === 1 && args[0] instanceof Array ) {
        array = args[0];
        n = args[0].length;
    } else {
        array = args;
    }
    var result = array[0];
    for( var i = 1; i < n; ++i ) {
         result = func( result, array[i] );
         if( result === false )
             return false;
    }
    return result;
}  

function MCD() {
    return Math.abs( plegar( MCD2, arguments ) );
}

function mcm() {
    return Math.abs( plegar( mcm2, arguments ) );
}

console.log( MCD( [36, 12, 96] ) );
console.log( MCD( 36, 12, 96 ) );
console.log( MCD( -36, -12, 96 ) );
console.log( MCD( [36] ) );
console.log( MCD( -36 ) );
console.log( mcm( [36, 12, 96] ) );
console.log( mcm( 36, 12, 96 ) );
console.log( mcm( -36, -2, 96 ) );
console.log( mcm( [36] ) );
console.log( mcm( 36 ) );