skip to Main Content

I am trying to write a function that returns the first odd number in an array.

I have this so far but it is returning undefined.

function findFirstOdd(numbers) {
  let oddNumbers = [];
  for (let i = 0; i <= numbers.length; i++) {
    if (numbers[i] % 2 != 0) {
      oddNumbers.push(numbers[i])
    } else {}
  };
  return oddNumbers[0]
};

console.log(findFirstOdd(6, 2, 9, 3, 4, 5));

I would expect this to return 9; being the first odd number.

4

Answers


  1. Your function call is wrong, numbers should be in array.

    console.log (findFirstOdd([6, 2, 9, 3, 4, 5]));
    
    Login or Signup to reply.
  2. Your code expects to receive an array as argument, but you are passing multiple numbers, not an array.
    There are two simple ways to solve this:

    function findFirstOdd(...numbers) {
    

    OR

    console.log(findFirstOdd([6, 2, 9, 3, 4, 5]));
    

    Also, one suggestion, you can remove empty else + you may return first odd number right after you found it
    Full code:

    function findFirstOdd(numbers) {
      for (let i = 0; i <= numbers.length; i++) {
        if (numbers[i] % 2 !== 0) {
          return numbers[i];
        }
      }
      return undefined;
    }
    
    console.log(findFirstOdd([6, 2, 9, 3, 4, 5]));

    UPD:
    After you learn array methods, you can upgrade it to:

    function findFirstOdd(numbers) {
      return numbers.find((number) => number % 2 !== 0);
    }
    
    console.log(findFirstOdd([6, 2, 9, 3, 4, 5]));
    
    Login or Signup to reply.
  3. numbers parameter most be an array:
    I cleaned your script into this:

    function findFirstOdd(numbers) {
      let oddNumbers = [];
      for(let i = 0; i < numbers.length; i++) {
        if (numbers[i] % 2 !== 0){
          oddNumbers.push(numbers[i]);
        } else {
          //do nothing
        }
      }
      return oddNumbers;
    };
    
    var numbers = [6, 2, 9, 3, 4, 5];
    
    console.log (findFirstOdd(numbers));

    I change some sections in your code:

    I changed entry of function into an array: var numbers = [6, 2, 9, 3, 4, 5];

    for(let i = 0; i <= numbers.length; i++) to for(let i = 0; i < numbers.length; i++)

    return oddNumbers[0] to return oddNumbers;

    semicolon ; on javascript codes are not necessary.

    Login or Signup to reply.
  4. JavaScript functions arguments can only relate to a single variable.

    In your case you have 2 choices:

    Rest parameters usage:

    function findFirstOdd(...numbers) {
      let oddNumbers = [];
      for (let i = 0; i <= numbers.length; i++) {
        if (numbers[i] % 2 != 0) {
          oddNumbers.push(numbers[i])
        } else {}
      };
      return oddNumbers[0]
    };
    
    console.log(findFirstOdd(6, 2, 9, 3, 4, 5));

    keyword arguments usage:

    function findFirstOdd() {
      let oddNumbers = [];
      for (let i = 0; i <= arguments.length; i++) {
        if (arguments[i] % 2 != 0) {
          oddNumbers.push(arguments[i])
        } else {}
      };
      return oddNumbers[0]
    };
    
    console.log(findFirstOdd(6, 2, 9, 3, 4, 5));

    PS: one line code ( odd numbers right bit have a binary value === 1 )

    const findFirstOdd = (...n) => n.find( n => n & 1);  
    
    console.log(findFirstOdd(6, 2, 9, 3, 4, 5));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search