0

I am writing a quick bit of code which find the first three even numbers in a random length of random numbers in an array.

  const getEven = (array) => {
  const evenArr = [];
  for (let index = 0; index < array.length; index++) {
    if (index % 2 != 0) {
      evenArr.push(array[index]);
    }
  }
  console.log(evenArr);
};

getEven([1, 2, 3, 4, 5, 6, 7, 8, 9]);
Output = [ 2, 4, 6, 8 ]

The question I have is, once it finds the first three even elements the loop continues to run, how can I stop it after finding the first three elements and is there a more efficient way of doing this?

I could use a filter but decided to go traditional and wrote a for-loop.

4 Answers 4

2

Try this

 if (index % 2 != 0 && evenArr.length<3) 
Sign up to request clarification or add additional context in comments.

1 Comment

This is really clean, and I never even thought of it! thanks.
1

Well, you may just add break statement with your desired condition:

 const getEven = (array) => {
  const evenArr = [];
  for (let index = 0; index < array.length; index++) {
    if (index % 2 !== 0) {
      evenArr.push(array[index]);
    }
    
    if (evenArr.length === 3) {
      break;
    }
  }
  console.log(evenArr);
};

getEven([1, 2, 3, 4, 5, 6, 7, 8, 9]);
Output = [ 2, 4, 6, 8 ]

1 Comment

Amazing, thanks Sergey. I did not know you could use a break with conditions. Great to learn something new!
1

With your current approach, the easiest change would be to add break -statement to your loop:

  for (let index = 0; index < array.length; index++) {
    if (index % 2 != 0) {
      evenArr.push(array[index]);
      if (arrray.length >= 3) {
        break; // <= this will end the loop
      }
    }    
  }

It is also good to mention that your algorithm is collecting odd numbers not even numbers currently. SHould be index % 2 === 0.

2 Comments

Thanks, great answer. i.e. Even numbers are those numbers that can be divided into two equal groups or pairs and are exactly divisible by 2. For example, 2, 4, 6, 8, 10, cuemath.com/numbers/even-numbers
Exactly my point. The code is adding values to the array when they are not even. ndex % 2 != 0 is true when the value is odd value. 2 % 2 = 0 // even 3 % 2 = 1 // odd ==> != 0 4 % 2 = 0 // event
1

Well you can use break

   if (evenArr.length === 3) {
      break;
   }

or return

   if (evenArr.length === 3) {
      return;
   }

or better:

   if (evenArr.length === 3) {
      return evenArr
   }

Return not just stops the loop but the whole function

const getEven = (array) => {
  const evenArr = [];
  for (let index = 0; index < array.length; index++) {
    if (index % 2 != 0) evenArr.push(array[index]);
    if(evenArr.length === 3) return evenArr;
  }
  return evenArr
};

Comments

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.