Dividing first number in function by second

Hello, need some kind of advise or a direction.

I need to write a function, that accepts two arguments (numbers), for example a and b. I need to write a loop that prints all numbers from 0 to a that can be divided by b.

This is what I have so far:

function myFunction(a, b) {
    let number = 0;
    for(var i = 1; i <= a; i += 1) {
    number = i;
}
    //so far, I am just printing all the numbers from 0 to 10
}
myFunction(10, 3);

The result should be like this:
0
3
6
9

Thanks!!

What do you think it means for when you say " can be divided by b"? You need to figure out what that means and then you can write code to check for it.

Why do you think the numbers 1, 2, 4, 5, 7, 8, and 10 are not supposed to be printed?

Well, all of those numbers cannot be divided by 3 (in this case).

What do you mean they can not be divided by 3? I can certainly divide 2 by 5. The result would be 0.4, so there must be some other characteristic you are looking for here. What else makes the numbers 0, 3, 6, and 9 special with respect to dividing them by 3?

We can divide them by themselves? Or maybe I am missing something :slight_smile:

The only number I can not divide successfully by itself would be 0. Actually go through the process of
of making each calculation for each number divided by 3 and see if you notice what those 4 particular numbers (0, 3, 6, 9) have in common when they are divided by 3.

Hint: You will notice the common connection within the first step in the calculation.

Yes, we are getting respectfully whole numbers (1,2,3). Is that correct?

That is true, but why are you getting whole numbers?

As I understand, because the rest of the numbers will get remainders, like 3,33333

Now you got it!

If you have not already done so, I strongly suggest completing the Basic JavaScript section of the Free Code Camp curriculum. You will learn about several special operators with one being the remainder operator which would definitely help you with your current problem.

Hello! so I was trying to break the code into smaller bits, and here is what I am starting with:

function myFunction(a, n) {
    for (let i = 0; i <= a; i++) {
        let results = i / n;
        console.log(results);
    }
}
myFunction(10, 3);

Basically I want to loop through, but I am missing a step, as I am trying to get atleast the 0, 1.333,2.666, 3 as the result. Can you advise what am I missing?

Thank you so much!

Read about the remainder operator. This will simplify what you should check.

Also, to make your code more readable, name the function, parameters and variables in such a way which describe what they represent. For example, I would do something like:

function displayEvenlyDivisibleNumbers(maxNumber, divisibleBy) {
  for (let number = 0; number <= maxNumber; number++) {
    if (condition to check if number can be divided evenly by divisibleBy) {
      console.log(number);
    }
  }
}

WOW! That worked. This is amazing! Let me show you my current code.

function displayEvenlyDivisibleNumbers(a, b) {
  for (let number = 0; number <= a; number++) {
    if (!(number % b)) {
      console.log(number);
    }
  }
}
displayEvenlyDivisibleNumbers(10, 3);

I am quite new to Javascript, but decided to learn it a bit differently. I want to find useful practice example, such as this one and try to solve it on paper and then find how to turn it to code.

Anyway! I appreciate your help! You are great!

You could also do: if (number % b === 0)) {

1 Like
// simply do this
function myFunction(a, b) {
    for(var i = b; i <= a; i += b) {
         console.log(i);
    }
}

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.