-2

The prompt is to find the index number of fourth occurrence of the number in an array. I am trying to implement break in the for loop statement, I am not sure how to make it work. Here is my code:

let array = [0,4,4,3,2,1,4,5,6,4,6,9];

for (i = 0; i <= array.length; i++){
    if (array[i] === 4){
        console.log("The fourth occurrence of 4 is:", i)
        break;
    }
}

Suppose output

The fourth occurrence of 4 is: 9
  • And the problem/question is? -> [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Aug 13 '21 at 17:36
  • Arrays are zero-based. The last element is at index `array.length - 1` and not `array.length`. – Andreas Aug 13 '21 at 17:40

7 Answers7

1

This will do the trick for you:

function getFourthOccurance(number){
    let ocurrences = 0;
    for (i = 0; i <= array.length; i++){
        if (array[i] === number){
            ocurrences++;
            if(ocurrences === 4){
                return i;
            }
        }
    }
}

let array = [0,4,4,3,2,1,4,5,6,4,6,9];
console.log("The fourth occurrence of 4 is:", getOccurance(4))
Pelicer
  • 1,348
  • 4
  • 23
  • 54
  • Thanks, this is what I been looking for. Just clarify, why we need to create another variable name occurrences? – VictorChars96 Aug 13 '21 at 21:35
  • You need to know how many times the value you've given has been found. On your question, array[i] === 4 is only asserting that the value in the i position inside array is equals 4, not how many times it has been found. Feel free to upvote the answer if it helped you. – Pelicer Aug 14 '21 at 14:15
0

Are you looking for this:

let array = [0,4,4,3,2,1,4,5,6,4,6,9];

let numberOfOccurencess = 0;
for (i = 0; i <= array.length; i++){
    if (array[i] === 4){
       numberOfOccurencess++;
    } 
   
   if(numberOfOccurencess === 4) {
      console.log('4th occurence is', i);
      break;
   }
}
Rahul
  • 5,594
  • 7
  • 38
  • 92
0

Please use this code.

let array = [0,4,4,3,2,1,4,5,6,4,6,9];
let count = 0;
const num = 4;
for (i = 0; i <= array.length; i++){
    if (array[i] === num){
        count++;
        if(count === 4) {
            console.log("The fourth occurrence of 4 is:", i)
            break;
        }
    }
}
Kirill Savik
  • 1,228
  • 4
  • 7
0

You should store count of occurrences in some variable.

let array = [0,4,4,3,2,1,4,5,6,4,6,9];
var occurrences = 0;
for (i = 0; i <= array.length; i++){
    if (array[i] === 4){
        occurrences += 1;
        if (occurrences === 4) {
            console.log("The fourth occurrence of 4 is:", i)
            break;
        }
    }
}
jnbm
  • 118
  • 2
  • 4
0

You can iteratively update the last index from the previously known one.

const indexOfNthOccurance = (arr, value, occurance = 1) => {
  let lastIndex = -1;
  while (occurance > 0) {
    lastIndex = arr.indexOf(value, lastIndex + 1);
    occurance--;
  }
  return lastIndex;
};

const array = [0, 4, 4, 3, 2, 1, 4, 5, 6, 4, 6, 9, 0, 1];

console.log(`The 4th occurrence of 4 is: ${indexOfNthOccurance(array, 4, 4)}`); //  9
console.log(`The 2nd occurrence of 0 is: ${indexOfNthOccurance(array, 0, 2)}`); // 12
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

You could use map/filter.

let array = [0, 4, 4, 3, 2, 1, 4, 5, 6, 4, 6, 9];

const indices = array.map((e, i) => (e === 4 ? i : -1)).filter(i => i !== -1);

console.log(`The fourth occurrence of 4 is at index ${indices[3]}.`);
norie
  • 9,609
  • 2
  • 11
  • 18
0

function nthInstanceFinder(arr, num, n){ 
  let counter = 0, target = n 
  let i = 0 
  if (target === 0 || arr.length === 0 || num === undefined) 
    console.log("check your input and try again") 
  while (counter < target && i < arr.length) {  
    if (arr[i]===num)
      counter +=1 
    i++ 
  } 
    if (counter===target) 
      console.log(target, " occurance of ", num, " is at index ", i-1) 
     else console.log( "Array does not contain ", target, " instance(s) of ", num) 
} 
  
let arr=[0,4,4,3,2,1,4,5,6,4,6,9]
let numberToFind=4 
let n=3 
nthInstanceFinder(arr, numberToFind, n)
DonCarleone
  • 544
  • 11
  • 20