1

I try to receive only even numbers, summing values from two arrays. If the result cannot be even, it should return one even value without summing being executed.
These are two arrays:

var a = [-2, 10, 30, 50, 11, 22, 100];
var b = [10, 8, 22, 5, 11, -5];

So, the result should be :

[8, 18, 52, 50, 22, 22, 100],

which stems from the following:

-2 + 10 = 8 (even), 10 + 8 = 18 (even), 
30 + 22 (even), 50 + 5 = 55 (not even), 

so 50 should be, because it is even, 11 + 11 = 22 (even), 22 + (-5) = 17 (not even), so 22 should be, because it is even, 100 is even.

I have the code, but it returns boolean "true" instead of 50 and second 22. How that can be fixed?

function sumArray(a, b) {
      var c = [];
      for (var i = 0; i < Math.max(a.length, b.length); i++) { 
        c.push((a[i] || 0) + (b[i] || 0));
      }
     
  return c;
}
    
var a = [-2, 10, 30, 50, 11, 22, 100];
var b = [10, 8, 22, 5, 11, -5];


var numbers = sumArray(a, b);
function OnlyEvens(numbers) {
 ;
  var evens = [];
  for(var i = 0; i < numbers.length; i++){
    if(numbers[i]%2 == 0) evens.push(numbers[i]);
    else (evens.push (a[i] % 2 == 0 || b[i] % 2 == 0))
  }
  return evens
}
var check = OnlyEvens(numbers);

console.log(check)

2
  • 1
    "but it returns boolean "true"" - Because that's what your else branch produces. If you compare something (a[i] % 2 == 0) the result will be either true or false. Commented Apr 15, 2021 at 12:21
  • @ Andreas, thank you. Commented Apr 21, 2021 at 10:15

3 Answers 3

1

When you tried to push the array a number in case it's not even, you actually pushed the result of the condition which will give you a boolean result true/false.

a[i] % 2 == 0|| b[i] % 2 == 0

What you can possibly do is add a ternary condition. if a[i] is even, then push that value to the array. else, push b[i] (since it must be even because uneven + uneven gives you an even number, hence it will enter the

a[i] + b[i] % 2 === 0 

condition on the first if.

function sumArray(a, b) {
      var c = [];
      for (var i = 0; i < Math.max(a.length, b.length); i++) { 
        c.push((a[i] || 0) + (b[i] || 0));
      }
     
  return c;
}
    
var a = [-2, 10, 30, 50, 11, 22, 100];
var b = [10, 8, 22, 5, 11, -5];


var numbers = sumArray(a, b);
function OnlyEvens(numbers) {
 ;
  var evens = [];
  for(var i = 0; i < numbers.length; i++){
    if(numbers[i]%2 == 0) evens.push(numbers[i]);
    else (evens.push (a[i] % 2 == 0 ? a[i] :  b[i] ))
  }
  return evens
}
var check = OnlyEvens(numbers);

console.log(check)

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

2 Comments

I would appreciate if you can upvote and accept the answer (:
I wish I could of course, but do not have enough reputation score. :(
0

You could check the remainder and get either both values or just a single one.

function sumEven(a, b) {
    return a.map((v, i) =>
        v % 2 === b[i] % 2
            ? v + b[i]
            : v % 2
                ? b[i]
                : v
    );
}

const
    a = [-2, 10, 30, 50, 11, 22, 100],
    b = [10, 8, 22, 5, 11, -5];

   console.log(...sumEven(a, b));

Another approach with finding the value.

function sumEven(a, b) {
    return a.map((v, i) => [v + b[i], v, b[i]].find(v => v % 2 === 0));         
}

const
    a = [-2, 10, 30, 50, 11, 22, 100],
    b = [10, 8, 22, 5, 11, -5];

   console.log(...sumEven(a, b));

Comments

0

You could recurse over the values of both arrays ( a and b ) and add the even total or even value to the new array. After that add the leftover values to the result ( assumed they can be both odd or even in the code below ).

var a = [-2, 10, 30, 50, 11, 22, 100];
var b = [10, 8, 22, 5, 11, -5];
let c = [];

while( a.length > 0 && b.length> 0 ) {
  let val_1 = a.shift();
  let val_2 = b.shift();
  let total = val_1 + val_2;
  
  if( is_even(total) )
    c.push( total );
  else if ( is_even(val_2) )  
    c.push( val_2 );
  else if ( is_even(val_1) ) // unnecessary check      
    c.push( val_1 );
}

// add leftover values (always)
c = c.concat(a).concat(b);

console.log(c);

function is_even( integer ) {
  return integer % 2 === 0;
}

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.