1
const arr = [{item:'a', value:false}, {item:'b', value:true}, {item:'c', value:true}, {item:'d', value:false}];
const startIndex = 2;

Given startIndex find next object in array with value=true. Iteration must be forward and, if not found, start from index 0. Should return arr[1];

I've just hit the wall and seems can't find an iteration method:/

let result = {};
for (let i = startIndex + 1; i < arr.length; i++) {
    if (!arr[i] && i < arr.length) {
        result = arr[i];
    }
}
console.log(result);
6
  • There is this function called indexOf it takes 2 parameters... look it up Commented Feb 15, 2021 at 22:03
  • 1
    @Akxe, this does only work with same object references. Commented Feb 15, 2021 at 22:03
  • Hint: You aren't checking the value property and not breaking if found Commented Feb 15, 2021 at 22:04
  • How do I start from the beginning when I reach end. Given for loop Commented Feb 15, 2021 at 22:04
  • 1
    Just use 2 loops: the first one from startIndex + 1 forward, if not found start the second one from 0 to startIndex. Why do you want it to be 1 loop? Commented Feb 15, 2021 at 22:07

3 Answers 3

1

You could take a single loop and take startIndex as offset and shape the index by the remainder with the length.

const
    array = [{ item: 'a', value: false }, { item: 'b', value: true }, { item: 'c', value: true }, { item: 'd', value: false }],
    startIndex = 3;
    
let result;

for (let i = startIndex; i < array.length + startIndex; i++) {
    console.log(i, i % array.length);
    if (array[i % array.length].value) {
        result = array[i % array.length];
        break;
    }
}

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

let i = startIndex + 1; produced desired results.
1

I'm not sure if I understood. But...

getNext = function(startIndex, arr){
    var i;
    for(i = startIndex; i < arr.length; i++){
        if(arr[i].value){
            return arr[i];
        }
    }

    if(startIndex != 0){
        return getNext(0, arr);
    }
    return null;
}

5 Comments

This works! But you have an error "i > arr.length" should be "i < arr.length". Thank you.
Yes! That's true XD Edited!
Great solution. But I would like to avoid recursion if possible.
Only one level of recursivity. You will not have a stack overflow at all.
I agree with you. But I prefer to keep recursion only for cases when there is a recursive data structure.
1

You can use .splice to create the two halves:

  • First, search for the element with value=true in the second half, if found, keep in mind to add the length of the first to the index
  • If not found in the second part, search in the first one

const getElementAtIndexFromStart = (arr=[], startIndex=0) => {
  // get index of element if exists starting from startIndex
  let index = arr.slice(startIndex+1).findIndex(({value}) => value===true);
  if(index!==-1) {
    // if found, add the start index to it to represent the position in in arr
    index = index + startIndex + 1;
  } else {
    // if not found in the second half, search in the first one
    index = arr.slice(0, startIndex+1).findIndex(({value}) => value===true);
  }
  return arr[index];
}

const arr = [{item:'a', value:false}, {item:'b', value:true}, {item:'c', value:true}, {item:'d', value:false}];

console.log('Start: 0 => ', getElementAtIndexFromStart(arr, 0));
console.log('Start: 1 => ', getElementAtIndexFromStart(arr, 1));
console.log('Start: 2 => ', getElementAtIndexFromStart(arr, 2));
console.log('Start: 3 => ', getElementAtIndexFromStart(arr, 3));
console.log('Start: 5 => ', getElementAtIndexFromStart(arr, 5));

7 Comments

Unless I am misreading something. It should return startIndex=0 -> arr[1]; si=1-> arr[2]; si=2 -> arr[0]; si=3-> arr[0]. Thank you for the effort.
@TadasV. so element at startIndex shouldn't be count? (si=1-> arr[2] instead of arr[1] - si=2 -> arr[0] instead of arr[2])
Should be: f(arr,0)=arr[1], f(arr,1)=arr[2], f(arr,2)=arr[0], f(arr,3)=arr[0]
But arr[0] has value=false
Should be: f(arr,0)=arr[1], f(arr,1)=arr[2], f(arr,2)=arr[1], f(arr,3)=arr[1]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.