1

This is the code but it returns in unidentified . It should return two arrays where the first one is a set of odd numbers in the original array and the second is odd

function evenAndOdd(array) {
    let odd = [];
    let even = [];
    for (i = 0; i < array.length; i++) {
        if (array[i] % 2 === 0) {
            even.push(array[i]);
        } else {
            odd.push(array[i]);
        }
    }
    return even,odd;
}
4
  • What do you mean by "it returns in unidentified"? A JS function can only return one value. Commented Oct 21, 2022 at 16:25
  • Probably best to understand about scope [in this case, of the arrays you define in the function] and to decide how you want the information returned - for example perhaps as an array of two arrays, or using arrays that already have been created outside the function itself or....? Commented Oct 21, 2022 at 16:26
  • Note you cannot return two separate values, you can return them in object or an array but not separately, eg return [even,odd]; Commented Oct 21, 2022 at 16:28
  • 1
    I think it should return odd. JavaScript is not Python, and , is an operator. What you could return though is [even,odd], and then destructure it later. Commented Oct 21, 2022 at 16:28

7 Answers 7

1

adding curly braces around your return values converts them into accessible objects

function evenAndOdd(array) {
    let odd = [];
    let even = [];
    for (i = 0; i < array.length; i++) {
        if (array[i] % 2 === 0) {
            even.push(array[i]);
        } else {
            odd.push(array[i]);
        }
    }
    return {even,odd};
}


console.log(evenAndOdd([1,2,3]))

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

Comments

0

You cant return two separate values from the same function. Instead, you can return a new array containing both of the other arrays.

function evenAndOdd(array){
  let odd = [];
  let even = [];
  for (i=0; i<array.length; i++){
    if (array[i] %2===0){
      even.push(array[i]);
    }else{
      odd.push(array[i]);
    }
  }
  return [even,odd];
}

Comments

0

If you're returning two or more variables, you can return them inside an array such as:

function evenAndOdd(array) {
  let odd = [];
  let even = [];
  for (i = 0; i < array.length; i++) {
    if (array[i] % 2 === 0) {
      even.push(array[i]);
    } else {
      odd.push(array[i]);
    }
  }
  return [even, odd];
}

console.log(evenAndOdd([1,2,3,4,5,6]));

Comments

0
function evenAndOdd(array) {
    let odd = [];
    let even = [];
    for (i = 0; i < array.length; i++) {
        if (array[i] % 2 === 0) {
            even.push(array[i]);
        } else {
            odd.push(array[i]);
        }
    }

    return [even, odd];
}

var [even, odd] = evenAndOdd([0,1,2,3,4,5,6,7,8,9])
console.log("Even " + even)
console.log("Odd " + odd)

Comments

0
  • You have to pass a valid number array.
  • and all I have done is just created an object with odd and even values array and return it because your code was only returning odd numbers.

function evenAndOdd(array) {
  let odd = [];
  let even = [];
  let length = array.length
  for (i = 0; i < length; i++) {
    if (array[i] % 2 === 0) {
      even.push(array[i]);
    } else {
      odd.push(array[i]);
    }
  }
  return {
    odd,
    even
  };
}

console.log(evenAndOdd([1, 2, 3, 4, 5, 6, 7, 8, 9]))

1 Comment

You can shortcut that into return {even, odd};.
0

As of ECMAScript 2024 there is a native function Object.groupBy for this:

const { 0: even, 1: odd } = Object.groupBy(arr, x => x % 2);

Example:

const arr = [10, 3, 9, 6, 12, 7];
const { 0: even, 1: odd } = Object.groupBy(arr, x => x % 2);
console.log("odd:", ...odd);
console.log("even:", ...even);

Comments

-1

arr[i] & 1 === 0 is more elegant and should run more quickly than arr[i] % 2 === 0 if you're working with a large array

1 Comment

arr[i] & 1 === 0 is never true, because === has precedence over &. Apparently you didn't check your answer before posting.

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.