I am currently facing a challenge with comparing non-consecutive indexes in an array. For instance, how can I compare index 0 with all the other indexes until reaching index 3 in a given array like this:
const arr = ["Juan", "Maria", "Maria", "Juan"]
In this scenario, comparing index 1 and 2 is easy, but how do I compare index 0 with all others sequentially until matching index 3?
Task Input: USA, AUSTRALIA, AUSTRALIA, INDIA, FRANCE, USA
Display the name of the country followed by "Bingo" if it is repeated consecutively
Display the name of the country followed by "Hooray" if it is repeated but not consecutive
Note: Utilize any Array method
Expected output: "Bingo Australia" "Hooray USA"
This is my attempt so far.
It should be noted that my current approach works because I am accessing countries[index + 5].
How can I dynamically increase the index after each iteration?
const countries = ['USA', 'Australia', 'Australia', 'France', 'India', 'USA'];
countries.forEach((value, index) => {
if (countries[index] === countries[index + 1]) {
console.log(`Bingo ${value}`);
} else if (countries[index] === countries[index + 5]) {
console.log(`Hooray ${value}`);
}
});