Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find non-matching numbers in an array?

This is number memory game in Reactjs


The first number appears randomly. For example: 123.

The second number is what I will enter: 123.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

That is correct.


So if I enter incorrectly for example 124, then 4 should be crossed out.

output should be like that:

<div>
  <span>1</span>
  <span>2</span>
  <span className="wrong">4</span>
</div>

More example:

random_num: 4573

input: 4674

output: 4674


random_num: 4573

input: 4674761

output: 4674761


random_num: 123

input: 123456

output: 123456

>Solution :

Assuming you are passing both the entries as arrays to a component and let say array_1 is the original entry and array_2 is the second entry, then all you got to do is

function App({ array_1, array_2 }) {
  return (
    <div>
      {array_2.map((val, i) => {
        if (val === array_1[i]) {
          return <span>{val}</span>;
        }
        return <span className="wrong">{val}</span>;
      })}
    </div>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading