0

I need to write a function that loops through an array of numbers, and returns the odd & even numbers in it's array.

I'm not sure if there's a better way to do this, and I'm stuck. Is there a way to return both statements?

var myNums = [1, 2, 3, 4, 5, 6, 7, 9];

var evens = []; 
var odds = [];  

function oddsAndEvens(nums) {
	for(var i = 0; i < nums.length; i++){
      if(nums[i] % 2 === 0){
        evens.push(nums[i])
      }
      else if (!nums[i] % 2 === 0) {
		odds.push(nums[i])
      }
    }  
      console.log(evens);
      console.log(odds);
    //I need it to "return" the array,
    //not console log
      
}
  
console.log(oddsAndEvens(myNums));

2
  • 1
    How about you do some basic research yourself? google.com/search?q=javascript+return+two+variables Commented Aug 29, 2017 at 16:24
  • 2
    You can simplify your code by using an else rather than an else if. If it's not even, it can only be odd! Commented Aug 29, 2017 at 16:32

4 Answers 4

1

A clean function to separate the evens from the odds.

function arrangeNums(array) {
  let odd = array.filter(i=>i%2!==0);
  let even = array.filter(i=>i%2===0);
  return {even:even,odd:odd};
}
console.log(arrangeNums([...Array(100).keys()]));

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

Comments

0

return arrays instead of console.log should work

var myNums = [1, 2, 3, 4, 5, 6, 7, 9];

var evens = []; 
var odds = [];  

function oddsAndEvens(nums) {
	for(var i = 0; i < nums.length; i++){
      if(nums[i] % 2 === 0){
        evens.push(nums[i])
      }
      else if (!nums[i] % 2 === 0) {
		odds.push(nums[i])
      }
    }  
    
     // return array of values you want
     return [evens, odds] 
}
  
console.log(oddsAndEvens(myNums));

Comments

0

You could use an object for the result and taken an array for the keys of the object to push the value.

function getGrouped(array) {
    return array.reduce(function (r, a) {
        r[['even', 'odd'][a % 2]].push(a);
        return r;
    }, { odd: [], even: [] });
}

var myNums = [1, 2, 3, 4, 5, 6, 7, 9];

console.log(getGrouped(myNums));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Off course you can, just return an object containing both evens and odds,

    function oddsAndEvens(nums) 
    {
      var evens = [];
      var odds = [];
      for(var i = 0; i < nums.length; i++){
          if(nums[i] % 2 === 0){
            evens.push(nums[i])
          }
          else if (!nums[i] % 2 === 0) {
    		    odds.push(nums[i])
          }
        }  
        return {"evens":evens,"odds":odds};   
    }
      
    var myNums = [1, 2, 3, 4, 5, 6, 7, 9];
    result = oddsAndEvens(myNums);
    console.log(result.evens);
    console.log(result.odds);

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.