-1

I'm trying to make a more advanced scientific calculator. I am having trouble figuring out how to do this type of calculation. I have an array of numbers and I want to check which of the given numbers are divisible by 3.

I would like both the collection of numbers divisible by 3 and the count of those numbers.

For example, on input of:

const collection = [
    { text: "0", value: 0 },
    { text: "1", value: 1 },
    { text: "2", value: 2 },
    { text: "3", value: 3 },
    { text: "4", value: 4 },
    { text: "5", value: 5 },
    { text: "6", value: 6 },
    { text: "7", value: 7 },
    { text: "8", value: 8 },
    { text: "9", value: 9 },
    { text: "10", value: 10 },
    { text: "11", value: 11 },
    { text: "12", value: 12 },
];

I would like to get output like:

"There are 5 divisible numbers by 3: 0, 3, 6, 9, 12"

How can I do that?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Navro
  • 145
  • 1
  • 6

5 Answers5

1

Use the modulo operator with for loops, denoted by %

Anuraag Barde
  • 399
  • 3
  • 9
1

I have an array of numbers and I want to check which of the given numbers are divisible by 3.

You have an array of objects holding a Number on the value key.


We can get the desired output by

  1. filter() only devisible by 3
    filter(c => c.value % 3 === 0)
  2. map() to only get number from value key
  3. join() to separate results with an comma

const collection = [{ text: "0", value: 0 }, { text: "1", value: 1 }, { text: "2", value: 2 }, { text: "3", value: 3 }, { text: "4", value: 4 }, { text: "5", value: 5 }, { text: "6", value: 6 }, { text: "7", value: 7 }, { text: "8", value: 8 }, { text: "9", value: 9 }, { text: "10", value: 10 }, { text: "11", value: 11 }, { text: "12", value: 12 }, ];

let res = collection.filter(c => c.value % 3 === 0).map(o => o.value);
console.log(`There are ${res.length} divisible numbers by 3: ${res.join(', ')}`);

Result

There are 5 divisible numbers by 3: 0, 3, 6, 9, 12

If we want to exclude 0, we can change te filter() to:

let res = collection.filter(c => c.value > 0 && c.value % 3 === 0).map(o => o.value);
0stone0
  • 34,288
  • 4
  • 39
  • 64
1

You can use filter method to filter out objects containing number divisible by 3, using modulo (%) operator. I have used map as well, to create an array of values, instead of objects.

const collection = [
    { text: "0", value: 0 },
    { text: "1", value: 1 },
    { text: "2", value: 2 },
    { text: "3", value: 3 },
    { text: "4", value: 4 },
    { text: "5", value: 5 },
    { text: "6", value: 6 },
    { text: "7", value: 7 },
    { text: "8", value: 8 },
    { text: "9", value: 9 },
    { text: "10", value: 10 },
    { text: "11", value: 11 },
    { text: "12", value: 12 },
];

let divby3 = collection.filter((x)=>x.value % 3 === 0).map((x)=>x.value)

console.log(`There are ${divby3.length} divisible numbers by 3: ${divby3}`)
TechySharnav
  • 4,869
  • 2
  • 11
  • 29
0

You can use the % operator. So if number%3 ==0 means it divides

Omle
  • 51
  • 5
0

Check out Remainder (%) operator.

Using it you can extract all numbers divisible by 3:

const divisible = collection.map(x => x.value).filter(x => !(x % 3))

And print them:

console.log(`There are ${divisible.length} divisible numbers by 3: ${divisible.join(', ')}`)
// There are 5 divisible numbers by 3: 0, 3, 6, 9, 12
Xeelley
  • 1,081
  • 2
  • 8
  • 18