Check that each element of an array is equal to the other. Javascript

1

I'm with an existential doubt.

I have a function, which must return true in case each element of an array is equal to the other.

I have an arrangement with five elements that constantly change, and every time a new element is set, the function already said is executed and it checks if all the elements are equal.

Up here everything is fine. The problem is that I made a set of conditional (copypaste), but it is a horrible way to write a code, so I turn to these forums to see what solutions they offer.

A functional demonstration similar to my code.

let contador = ["juan", "juan", "juan", "juan", "juan"]

function bantrue() {
  if (contador[0] == contador[1]) {
    if (contador[1] == contador[2]) {
      if (contador[2] == contador[3]) {
        if (contador[3] == contador[4]) {
          return true
        }
      }
    }
  }
}

if (bantrue() === true){
 document.write("bantrue() es true.");
}

//PERO SI ES FALSO 

let contador2 = ["juan", "Manuel", "juan", "juan", "juan"]

function bantrue2() {
  if (contador2[0] == contador2[1]) {
    if (contador2[1] == contador2[2]) {
      if (contador2[2] == contador2[3]) {
        if (contador2[3] == contador2[4]) {
          return true
        }
      }
    }
  }
}

if (bantrue2() === true){
 document.write("true");
} else {
  document.write(" ------- bantrue2() es false.");
}
  

As you can see, the first function returns true because the array has all "john", but the second array has a "manuel", so the conditionals stop.

What if I want to increase the amount of items that counter [] stores? Do I have to copy and paste the ifs five times more? I do not see it practical.

I would greatly appreciate other solutions to perform this check. From already thank you very much.

    
asked by Lottie 03.05.2018 в 05:06
source

2 answers

0

The necessary thing would be to apply a cycle that compares the value in position i with the next position (i + 1), I leave you an example using for but it also comes out with a while. I hope it helps.

let contador = ["juan", "juan", "juan", "juan", "juan"]
let Tam = contador.length;
var igual = false;
function bantrue() {
  for ( let i = 0; i < Tam-1; i++){   
    if (contador[i] == contador[i+1]) {
        igual = true
    }else{
      igual = false
      break
    }
  }
  return igual
}

if (bantrue() === true){
 document.write("bantrue() es true.");
}else{
  document.write("bantrue() es false.");
}
    
answered by 03.05.2018 / 06:45
source
0

And if tomorrow you get an array with a million positions? Would you write a million% of if's xD?

I would advise you to make the comparison using the cycle WHILE , where you only cycle while equality follows, as soon as you find an inequality, the cycle is cut and the program is finished, returning a TRUE value.

For example:

let contador = ["juan", "juan", "juan", "juan", "juan"]

In this case, go through the entire array to be sure that all the values are the same.

let contador2 = ["juan", "Manuel", "juan", "juan", "juan"]

And in this case as soon as the program sees that "Juan" is different from "Manuel", the cycle will be cut and he will not cycle anymore.

The code would be more or less like this:

index = 0;
indexSiguente = 1;

while(array.length > indexSiguente-1 && array[index] == array[indexSiguente]){
 index++;
 indexSiguente++;
}

return (indexSiguente < array.length)

    
answered by 03.05.2018 в 06:17