Has Only Odd Number

Has Only Odd Number

Odd numbers are whole numbers that cannot be divided exactly into pairs. Odd numbers, when divided by 2, leave a remainder of 1. 1, 3, 5, 7, 9, 11, 13, 15 … are sequential odd numbers. Odd numbers have the digits 1, 3, 5, 7 or 9 in their ones place.

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

function hasOnlyOddNumbers(arr){
  return arr.every(function(val){
    return val % 2 !== 0;
  })
}
console.log(hasOnlyOddNumbers([1,3,5])) true
console.log(hasOnlyOddNumbers([1,3,4])) false


We create a function called hasOnlyOddNumbers with parameter called arr

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

We return arr.every(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 different than 0;


comments powered by Disqus