Skip to content
Advertisement

How can one check if an integer is equal to another in a 2D array?

How can one check if an integer is equal to another in a 2D array?

JavaScript

I am trying to compare the integer “a” from the 2D array named “int[][]board” with the other variables (b, c, d, e, f, g, h, i) to check if “a” is equal to any of them.

I attempted to solve this issue by writing out this:

JavaScript

It seems like the operation || (known as “or”) cannot be used to compare integers. How can I resolve that issue?

Advertisement

Answer

What you could do is iterate through the 2d array and have a boolean to check if it contains the element you are comparing with you could write something as follows:

JavaScript

after these line of codes, you can progress to do as you please with the code

JavaScript

However, this will be always true if you try to compare variable “a” since the first element of your array is itself. What you could do for such situtaion is as following:

JavaScript

hope it helped.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement