Click here to Skip to main content
15,034,131 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to compare two arrays( containing 3 integers) and return a 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 :
Array1 = [2,7,6]
ReferenceArray=[297]
MessageArray = [“Almost”, “Not Quite”]

What I have tried:

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");
  }
Posted
Updated 14-Sep-21 10:49am
v2
Comments
Richard Deeming 14-Sep-21 13:00pm
   
There's a secret error somewhere in your secret code. You need to fix that.

Seriously, how do you expect anyone to help you fix your code when we can't see your code? Click the Improve question link and update your question to include the relevant parts of your code, along with the precise details of what the problem is and where you are stuck.

On the other hand, if you were expecting somebody here to write the code for you, you've come to the wrong site.
Patrice T 14-Sep-21 13:14pm
   
The whole problem is that if tell him the secret correction, you will have to kill him after :)

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Then think about how you would do it manually - find a way that works on paper, then write down the instructions to do that. Test them on several different data samples, and when you have something that works consider how to computerise it.
   
Comments
[no name] 14-Sep-21 16:51pm
   
Thank you for your nicely worded advice. I didn't expect my answer to get a response so quick I wasn't able to upload my code at the time. Post has been updates

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900