How to compare 2 array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hippy

Hi, I’m making a guessing game in which the player has to guess the capital city. I have the capitals in one array and the states in the other.
I have 3 buttons in the gui and on each button a capital city is randomly generated from array. But now I don’t know how to evaluate if the player picked the right city. I wanted to compare the positions of the elements to determine the right/wrong answer, but I don’t know how to do it .

var capitals = [Prague,Paris,Berlin]
var country = [Czech,France,Germany]

Can someone advise me how to do these things.

:bust_in_silhouette: Reply From: Alan Beetale

if country[number] == capitals [number]:

Being ‘number’ the number you randomly generated previously

Here you use the relational operator == that compares if two variables are equal and returns true if so. Make sure both of your arrays store the same data type (strings I assume for your case). You probably may be interested on this article: Operators & Operands | Godot GDScript Tutorial | Ep 02 | Godot Tutorials

Based on the OP’s original array contents, the suggestion here won’t work as it’ll result in comparing a given country’s string name to its capital city string name. So, the two will never be equal.

I assume what @Alan Beetale meant was to compare the two array indices to each other - so if both were1 for example, that’d be a match since Paris is the capital city of France.

While a solution to the problem should be relatively easy here, it really depends on how your code / data structures are organized. If you don’t get a reasonable suggestion here, posting the relevant code would be helpful for a more targeted answer.

jgodfrey | 2023-02-22 22:35