Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

H.C.F Calculation Program

/*
	JavaScript H.C.F program by lolman_ks.
    Logic is very nicely explained throught comments in the code.
*/

function gcd(){
    if(arguments.length == 0) return 1;
	if(arguments.length == 1) return arguments[0];

	var common_factors = [];
	var counter_1 = 1; //For testing different numbers as GCD.
	var counter_2 = 0; //For looping through arguments.

	//Loop for testing different numbers from 1 to minimum from arguments.
	while((counter_1 <= Math.min.apply(null , arguments))){
		//Check for exact divisibility of arguments[counter_2] by counter_1.
		if(arguments[counter_2] % counter_1 == 0){
			++counter_2; //Move to the next item in the array.
		}
		//If not exactly divisible.
		else{
			++counter_1; //Test the next number to be tested as GCD.
			counter_2 = 0; //Move to the first element of arguments.
		}
		/*
			counter_2 will be == arguments.length, if its value is not
			reset and the first condition is true for each element of 
			the array.
			Therefore, the number will be a common factor.
		*/
		if(counter_2 == arguments.length){
			common_factors.push(counter_1); //Add counter_1 to common_factors array.
		}
	}
	return Math.max.apply(null , common_factors);
}

/*
	I hope my answers are useful  to you, please promote them if they are.
    #lolman_ks.
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs how to beautify mysql arrays 
Javascript :: decrypt javascript code 
Javascript :: nextjs update ui when data is updated 
Javascript :: accessing state in nuxt vuex 
Javascript :: imleç 
Javascript :: date et heure javascript 
Javascript :: &amp;&amp; in react jsx 
Javascript :: JS mixin implementation 
Javascript :: convert low high to integer in js 
Javascript :: get first and last word initials from name 
Javascript :: Ghost-Blog MySQL install was not found 
Javascript :: re-resizable react example 
Javascript :: weakset use cases javaScript 
Javascript :: react using props and parent state 
Javascript :: asynchronous file reading 
Javascript :: Will Yield function Model 
Javascript :: i18next plural not working 
Javascript :: javascript destructure multiple levels 
Javascript :: unreachable code detected javascript 
Javascript :: convert js to tsx 
Javascript :: Scale to fit 
Javascript :: LeagueFlysystemAwsS3v3AwsS3Adapter 
Javascript :: javascript enum includes value 
Javascript :: NodeJS Multi-Core Processors Example 
Javascript :: starting: intent error type 3 react-native 
Javascript :: reverse an array in javascript 
Javascript :: shell curl path of json file as parameter without temporal file 
Javascript :: creating a react app from scratch 
Javascript :: wordpress how to read jquery 
Javascript :: copy one cell value to another in google app script 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =