-1
I am using a function that returns a multidimensional array in which each index contains an array of prime factorization.
This function, in turn, can accept a variable amount of arguments and returns the mdc of how many numbers are passed as arguments.
Ex: mdc(60,100,150)
returns an array mdcOperands = [ [2,2,3,5], [2,2,5,5], [2,3,5,5]];
I want to compare the different arrays within mdcOperands
to return a new array with the mdcOperands[i][j]
repeating at all. I tried to make a vis a vis comparison using a nested for loop but it compares only mdcOperands[i][j]
with mdcOperands[i+1][j]
, but there are cases where this comparison does not return an element, but it may be that the mdcOperands[i][j]
repeat itself in mdcOperands[i+1][j+2]
for example, as in mdcOperands[0][3]
, mdcOperands[1][2]
and mdcOperands [2][2]
.
I wanted to know if there is an easy way to compare these factored numbers arrays with i
variable or if I could transform the results into strings and compare the repetition simultaneously in all strings, perhaps using a regex that accepts a variable as a match value, e.g.:
'2235'
'2255'
'2355'
and return '25', which I could turn into an array [2,5]
and return to function mdc(60,100,150) = 2 * 5 = 10 (mdc dos três numeros)
. Remembering that the function mdc()
that I defined accepts arguments from 1 up to the maximum possible.
Edit: Excerpt from Upado Comments.
function compareFactors(mdcOperands) {
let comparingFactors = [].slice.call(arguments, 0);
let mdcFactors = [];
for (let mdcOperandsIndex = 0; mdcOperandsIndex < mdcOperands.length - 1; mdcOperandsIndex++) {
for (let j = 0; j < mdcOperands[mdcOperandsIndex].length; j++) {
if (mdcOperands[mdcOperandsIndex][j] === mdcOperands[mdcOperandsIndex + 1][j]) {
mdcFactors.push(mdcOperands[mdcOperandsIndex][j])
}
}
}
}
what have you tried to do to resolve? Post your code so we can help
– Alexis Garcia
Function compareFactors(mdcOperands) { //Let comparingFactors = [].slice.call(Arguments, 0); Let mdcFactors = []; for (Let mdcOperandsIndex = 0; mdcOperandsIndex < mdcOperands.length -1; mdcOperandsIndex++) { for (Let j = 0; j < mdcOperands[mdcOperandsIndex].length; j++) {
 if (mdcOperands[mdcOperandsIndex][j] === mdcOperands[mdcOperandsIndex+1][j]) {
 mdcFactors.push(mdcOperands[mdcOperandsIndex][j])
 }
 }
 } }
– Augusto M. Magalhães
It was bad, I started learning programming a little while ago and I don’t know how to use stackoverflow, I ended up here because I didn’t find anything anywhere, at least not with concepts that I know how to use to research. The closest I thought of this would be to turn it into strings and use Regex (which I have no idea how to operate) and do some kind of match between 2 strings WHILE variable, tpo str1.match(str2) but always returns null (I tried in the browser console).
– Augusto M. Magalhães
Go straight to the point and you don’t have to explain that you’re a beginner. Format the text of the question with markdown. See [Ask], [help], Stack Overflow Survival Guide in English, Community FAQ and do our [tour].
– Augusto Vasques
Thanks @Augustovasques
– Augusto M. Magalhães
I think you’re complicating something simple. See this example of function of the calculation of the mdc between n numbers.
– Augusto Vasques