6

I want to write a function that takes array as an argument and returns how many numbers can be divided by 12. However, if array has a number higher than 111 then it should return 0; I wrote this:

function isDivisble(array) {
  let numberOfNum = 0;
  for(let i=0; i < array.length; i++) {
    if(array[i] % 12 == 0 && array[i] < 111) {
      numberOfNum = numberOfNum + 1 ;
    } else {
      numberofNum = 0;
    }
  }
  return console.log(numberOfNum);
}

let test = [12, 24, 36, 44, 55, 255];


isDivisble(test)

I realized that this code checks individually whether the current number is divisible and not higher than 111 and not globally whether the array has a number higher than 111, but I dont understand how to make a general array check. Is writing for loop with if statement to check and then another for loop inside if statement makes it a little bit spaghetti?

lejlun
  • 4,140
  • 2
  • 15
  • 31
Maivan Fildero
  • 177
  • 1
  • 2
  • 8

5 Answers5

9

You can use some to check if there is any element which is greater than 111 first. Then you can use filter to get element that is divisible by 12. Like this:

const isDivisible = (arr) => {
    if (arr.some((e) => e > 111)) return 0;
    return arr.filter((e) => e % 12 === 0).length;
};
const test = [12, 24, 36, 44, 55, 48];
console.log(isDivisible(test));
5

I've slightly modified your function that returns 0 if a number is greater than 111 else it checks if it is divisible by 12

function isDivisble(array) {
  let count = 0;
  for(let i=0; i<array.length; i++){
    if(array[i] > 111){
        return 0;
    }else{
        if(array[i] % 12 === 0){
        count++
      }
    }
  }
  return count;
}


let test = [12, 24, 36, 44, 55, 255];
console.log(isDivisble(test));
ahsan
  • 1,409
  • 8
  • 11
3

The some array method will do the trick:

array.some(value => { return value > 111 }) will return true if any of its values is greater than 111.

You can also check if every array value respects a certain condition by using array.every with a similar callback:

array.every(value => { return value <= 111 }) is true only if every value in the array is lower than or equal to 111.

Samuel
  • 49
  • 1
  • 6
1

The best generic solution is the oene below Logic

  • Filter the array for number greater that 111. If this filter returns an array with a length greater than 1 then return 0
  • Else filter the array for numbers divisible by 12. No need to check number is less than 111. Because if that number is greater than 111 the function might have already returned 0.
  • Retun the length of the above filter.

Working Fiddle

function isDivisbleGeneric(arr) {
    const numberGreaterThanLimit = arr.filter((node) => node > 111);
    let returnCount;
    if(numberGreaterThanLimit.length > 0) {
        returnCount = 0;
    } else {
        const divisibleBy12 = arr.filter((node) => node %12 === 0);
        returnCount = divisibleBy12.length;
    }
    return returnCount;
}
let test = [12, 24, 36, 44, 55, 255];
const count = isDivisbleGeneric(test);
console.log(count);

One Line Solution

const isDivisbleOneLine = (arr) => arr.filter((node) => node > 111).length > 0 ? 0 : arr.filter((node) => node %12 === 0).length;
let test = [12, 24, 36, 44, 55, 255];
const count = isDivisbleOneLine(test);
console.log(count);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • The OP states that the function should return 0 if any single member of the array is greater than or equal to 111. – Pointy Aug 15 '21 at 12:32
-1

function isDivisble(array) {

  // at the beginning, do from here
  if (array.some(p=>p > 111)) {
    return 0;
  }

  // Rest xxxxxx
}
Xin
  • 33,823
  • 14
  • 84
  • 85