Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
How do I compare two arrays for different message outcomes?

I am trying to compare two arrays( containing 3 integers) and return a hint message array that conform to the logic

-Push “Almost” when 1 digit and position match in array

-push “not quite” when 1 digit matches but different position

-push “incorrect “ when no digits match

  • push “correct” When exact match

Example of arrays :

Array1 = [2,7,6]

ReferenceArray= [2,9,7]

Hint= [“Almost”, “Not Quite”];

Code I have so far:

    function check( array1, referenceArray ) {
    let hint=[];
  for(i=0;i<referenceArray.length;i++){

    for (j=0;j<Array1.length;j++){

      //value and position match
      if ((referenceArray[i] && reference.indexOf[i]) === (Array1[j] && Array1.indexOf[j])) {
      return hint.push('almost');       
      } 
 //value matches but not position
        else if(( referenceArray[i] ===Array1[j]) && !(referenceArray.indexOf[i]===Array1.indexOf[j] )){
        return hint.push('not quite');
        }   
    }// end of Array1 iteration
  } // end of reference  interation

    // if all values and position match
    if(referenceArray===Array1){
    return hint.push("correct");
  }
//if no values match
  else if (referenceArray!==Array1){
    return hintArray.push("incorrect");
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I did this code, tell me if it works or not 😁

const array1 = [2,7,6]
const ReferenceArray = [2,9,7]

function compareArrays(arr){
    let perfect = true
    for(let i = 0; i < ReferenceArray.length; i++){
        if(ReferenceArray[i] != arr[i]) {
            perfect = false
            break
        }
    }
    if(perfect) return 'correct'
    let hint = []
    for(let i = 0; i < ReferenceArray.length; i++){
        if(arr[i] == ReferenceArray[i]) hint.push('Almost')
        else if(ReferenceArray.includes(arr[i])) hint.push('Not Quite')
    }
    if(hint.length > 0) return hint
    return 'incorrect'
}

console.log(compareArrays(array1))

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would use some built in Array methods to help achieve this: every(), map() and findIndex().

I generally avoid using .push() because it mutates the array. Immutable code is nice to read 😉

const check = (array, referenceArray) => {
  if (array.every((val, index) => val === referenceArray[index] )) {
    return ['Correct']
  }

  const allHints = array.map((val, index) => {
    const refArrayIndex = referenceArray.findIndex(refVal => val === refVal);
    if (refArrayIndex === index) {
      return 'Almost'
    }
    if (refArrayIndex !== -1) {
      return 'Not Quite'
    }
    return undefined 
  });
  
  const hints = allHints.filter((hint) => hint !== undefined);
  if (hints.length > 0) {
    return hints;
  }
  return ['Incorrect']
};

const hints = check([2,7,6],[2,9,7]);
console.log('hints', hints)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda