Home > Blockchain >  How to compare different indexes in array using Javascript array methods
How to compare different indexes in array using Javascript array methods

Time:12-09

I'm trying to solve the problem below but I'm stuck in knowing how to compare one index with another that's not consequently for example:

const arr = ["Juan", "Maria", "Maria", "Juan"]

In this case, comparing index 1 and 2 would be simple, but how can I pick index 0 and then compare it with each one of the others until it get to index 3 to be matched?.

Exercise Input: USA, AUSTRALIA, AUSTRALIA, INDIA, FRANCE, USA

Print out the name of the Country and word "Bingo" if the element (Country name) is repeated and located one after another

Print out the name of the Country and word "Hooray" if the element (Country name) is repeated and located not consequently

Note: Use any Array method

Expected result: "Bingo Australia" "Hooray USA"

This is what I've tried.

Note that if I run it that way it would work but only because I'm accessing countries[index 5].

How can I make the index to dynamically increases itself when the iteration finishes?

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}`);
    }
});

CodePudding user response:

You can try something like this, it is not most efficient but it will solve your problem:

const countries = ['USA', 'Australia', 'Australia', 'France', 'India', 'USA'];

countries.forEach((value, index, arr) => {
  arr.forEach((val, idx) => {
    if (val === value && index < idx) {
      if (idx === index   1) console.log(`Bingo ${value}`);
      else console.log(`Hooray ${value}`);
    }
  });
});

CodePudding user response:

Solution with a lot of 'if' (just reduce iterations (loop count) Wrote with purpose with for loop, in reality, it is same as a nested loop.

const c_arr = [1, 4, 5, 5, 6, 7, 4, 1];
let j = 0;
let full_count = 0; //count loops

for (let i = 0; i < c_arr.length;   i) {
  if (j === 0 & c_arr[i] === c_arr[i   1]) { // condition j ==0 so it will go inside only in first loop
    console.log('Bingo', c_arr[i])
  }

  // console.log("Test", c_arr[j]);
  if (i   2 < c_arr.length & c_arr[j] === c_arr[i   2]) { // i 2 cond, to avoid to access elem. outside array
    console.log('Hooray', c_arr[i   2]);
    c_arr[j] = null;
  }

  // All to reduce loop count
  if (j > 0 & c_arr[j] === null) { // allneigbourgh elements are checked
    i = j;
      j;
  }
  if (j > 0 & i === c_arr.length - 3) { // avoid empty loop, condition for Hooray is i 2
    i = j;
      j;
  }
  if (i === c_arr.length - 1) { // on first loop increase j
    i = j;
      j;
  }
  // to stop loop when J reaches arr :-2 element
  if (j === c_arr.length - 2) {
    i = c_arr.length;
  }
    full_count;
}

console.log(full_count, " :loops count");
  • Related