Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
Numbers divisible by 3 in JavaScript

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use the modulo operator with for loops, denoted by %

about 4 years ago · Juan Pablo Isaza Report

0

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);
about 4 years ago · Juan Pablo Isaza Report

0

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}`)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!