Has Only Even Number

Has Only EvenNumber

Even numbers are whole numbers that cannot be divided exactly into pairs. Odd numbers, when divided by 2, leave a remainder of 0. Even numbers have the digits 2, 4, 6, 8 in their ones place.

Example:
console.log(hasOnlyEvenNumber([1,2,4,5])); true
console.log(hasOnlyEvenNumber([1,3,5])); false

function hasOnlyEvenNumber(arr){
  return arr.some(function(val){
    return val % 2 === 0;
  })
}

let arr= [1,2,3,4,5,6,7,8,9,10];
console.log(hasOnlyEvenNumber(arr)); true


We create a function called hasOnlyEvenNumber with parameter called arr

We return the arr.every every is build in JavaScript method that needs all to be true from the array

We return arr.some(function(val){
return val % 2 === 0})
This means that we take the arr argument use the some method and iterate and check if val % (remainder operator) 2 is equal to remainder 0;


comments powered by Disqus