1

I wrote the following code which works for cases with the array has more than one element. How could I make it work when the array consist of just one element?

The below works for [1,23,4,5] but not for [0], [2], 1

function oddOrEven(array) {
  var sum = array.reduce(function(acc, intialvalue) {
    return acc + intialvalue;
  });

  if (sum % 2 == 0) {
    return "even"
  } else {
    return "odd"
  }
}

console.log(oddOrEven([1,23,4,5]))
console.log(oddOrEven([0]))
console.log(oddOrEven([1]))

Please check this screenshot

6
  • Just do a check if the array length is 1, if so, return the value of that array. If not, do the rest of your code. Commented Mar 15, 2019 at 14:59
  • 5
    Please elaborate question because [0], [2], [1] are returning as expected Commented Mar 15, 2019 at 15:00
  • 1
    I've edited to include a snippet, but it seems to be working, perhaps I've interpreted the [0], [2], [1] incorrectly? Commented Mar 15, 2019 at 15:01
  • function oddOrEven(array) { var sum = array.reduce((acc, intialvalue) => acc + intialvalue); return (sum % 2 == 0) ? "even" : "odd" } Commented Mar 15, 2019 at 15:01
  • @OliverRadini - maybe he is using number directly, not using array - console.log(oddOrEven(1)) Commented Mar 15, 2019 at 15:03

6 Answers 6

5

As the error says, you must have an initial value, 0 in our case.

function oddOrEven(array) {
  var sum = array.reduce(function(acc, intialvalue) {
    return acc + intialvalue;
  }, 0);

  if (sum % 2 == 0) {
    return "even"
  } else {
    return "odd"
  }
}

console.log(oddOrEven([1,23,4,5]))
console.log(oddOrEven([0]))
console.log(oddOrEven([1]))

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

Comments

2

No initial value means you need to set the initial value of the reduce funtion as 0.

function oddOrEven(array) {
  var sum = array.reduce(function(acc, value) {
    return acc + intialValue;
  }, 0); // <- set the reduce functions initial value

  if (sum % 2 == 0) {
    return "even";
  } else {
    return "odd";
  }
}

You can clean this code up a little by re-writing it as

const oddOrEven = (array) => {
  const sum = array.reduce((acc, value) => acc + value, 0);

  return (sum % 2 == 0) ? "even" : "odd";
};

2 Comments

Hi VtoCorleone, whats the name of the following operator -->(sum % 2 == 0) ? "even" : "odd";///<--
@jon Its called ternary operator
2

Give an initial value to your reduce call. You could also handle the case of an empty array, here I return undefinef:

const oddOrEven = arr => arr.length
  ? arr.reduce((sum, x) => sum + x, 0) % 2 === 0 ? 'even' : 'odd'
  : undefined;

console.log(oddOrEven([1, 23, 4, 5]))
console.log(oddOrEven([0]))
console.log(oddOrEven([1]))
console.log(oddOrEven([]))

2 Comments

Hi jo_va, what is the name of this -->A ? "even" : "odd" <-- operator?
@jon, this is a ternary operator, the condition is before the question mark, if it is true, the part before the semicolon is evaluated, otherwise, the part to the right of the semicolon is evaluated
2

As others have pointed out, you will need to pass an initial value to reduce.

As an alternate solution, you could just count whether there are are an even or number of odd elements in the array.

const oddOrEven = (array) => array.reduce((a, i) => i % 2 ^ a, 1) ? "even" : "odd"; 

console.log(oddOrEven([1,23,4,5]))
console.log(oddOrEven([0]))
console.log(oddOrEven([1]))

Consider the following truth table:

x    y    | x+y
----------|-----
even even | even
even odd  | odd
odd  even | odd
odd  odd  | even

Comments

1

Bitwise & can be used to check even and odd

function oddOrEven(array) {
  var sum = array.reduce((op, inp) => op + inp, 0);
  return sum & 1 ? 'odd' : 'even' 
}

console.log(oddOrEven([1,23,4,5]))
console.log(oddOrEven([0]))
console.log(oddOrEven([1]))

For the one-line lovers

const oddOrEven = a => a.reduce((o,i) => o + i, 0) & 1 ? 'odd' : 'even' 

console.log(oddOrEven([1,23,4,5]))
console.log(oddOrEven([0]))
console.log(oddOrEven([1]))

Comments

0
whether the sum of array elements is even or odd and if array is empty its even.. 
function evenandOdd(arr) {
    let sumofarr = 0;
            for(i=0; i < arr.length; i++) {
        sumofarr = sumofarr + arr[i];
        }  //return sumofarr;
        if(sumofarr % 2 == 0 || arr.length == 0) 
            return 'even'; 
            else return 'odd';
} 
console.log(evenandOdd([-102, -1, 3]));

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.