Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!