Division and Modulo

So far, we have shown how standard arithmetic operations like addition and multiplication work...

Before we dive into division, it's important to note that in programming languages, decimal numbers are written with a period instead of a comma. So, for example, instead of 1,5 we write 1.5. This is important to remember to avoid unnecessary errors.

Now let's look at division and the remainder after division. When you perform division in JavaScript using the / operator, the result will be what you would expect, e.g.:

console.log(3 / 2); // prints 1.5
console.log(4 / 2); // prints 2.0
console.log(1 / 0.5); // prints 2.0

But what if we want to find out the integer remainder? For that, we use the modulo operator - %. For example:

console.log(3 % 2); // prints 1, because 3/2 = 1, remainder 1
console.log(4 % 2); // prints 0, because 4/2 = 2, remainder 0
console.log(1 % 0.5); // prints 0, because 1/0.5 = 2, remainder 0

Instructions

On the second line, create a variable inWallet with a value of 100.

On the third line, print out how much money you will have left after buying as much chocolate as possible using the command:

console.log(inWallet % priceOfChocolate);

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/10