Show elements of an array if they are repeated and if they match your position

0

I have this script currently showing me the repeated numbers between 2 arrays but I can not do it to show me the repeated numbers and if their position matches

var number_load = [2,3,4,1]
var number_input = [9,5,3,1]

for(var numbers in number_load){
    if(number_input.indexOf(number_load[numbers])>-1){
       console.log('Se repite el numero : ' + number_load[numbers])
    }
}
    
asked by Oscar Diaz 21.09.2017 в 19:04
source

2 answers

1

If you are not planning to do this procedure in gigantic arrangements and if the two arrangements will always be the same size, I think you can go through the arrangements manually.

var number_load = [2,3,4,1];
var number_input = [9,5,3,1];

for(var i = 0; i < number_load.length; i++){
    for(var j = 0; j < number_input.length; j++){
        if(number_load[i] == number_input[j])
            if(i == j)
                console.log('Se repite el numero : ' + number_load[i] + ' en la posición '+ (i + 1))
            else
                console.log('Se repite el numero : ' + number_load[i] + ' pero su posicion no coincide ')
    }
 }

Note that we are comparing the first array against the second and that the position shown in the message is for end users (the true position plus one).

    
answered by 21.09.2017 / 20:05
source
0

Here you have the solution you should compare the position of the element in the two vectors with indexOf and see if they match.

var number_load = [2,3,4,1]
var number_input = [9,5,3,1]

for(var numbers in number_load){
    if(number_input.indexOf(number_load[numbers])>-1){
       console.log('Se repite el numero : ' + number_load[numbers])
       if(number_input.indexOf(number_load[numbers]) == 
       number_load.indexOf(number_load[numbers])){
       console.log("coincide lugar del numero " + number_load[numbers]);
    }
     
}

}
    
answered by 21.09.2017 в 19:54