TL ; DR:
The Math.floor()
function returns the largest integer less than or equal to a given number:
console.log(Math.floor(3.9)); // Output: 3
console.log(Math.floor(5.2)); // Output: 5
console.log(Math.floor(7)); // Output: 7
We can use this function if we want to compute the integral part of some division (quotient):
let a = 3;
let b = 7;
console.log(Math.floor(20 / a)); // Output: 6
console.log(Math.floor(b / a)); // Output: 2
Full lesson:
Remember how we first learn about the division of two integer numbers in primary school?
The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.
Here are some examples:
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
Floor division
Floor division (//
) is a normal division operation except that it returns the integral part of the result (the quotient):
print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6
It can also be used with variables:
a = 3
b = 7
print(20 // a) # Output: 6
print(b // a) # Output: 2
Modulo
The modulo operator (%
) calculates the remainder of dividing two values:
print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2
# Can be used with variables:
a = 2
b = 4
print(b % a) # Output: 0
print(11 % b) # Output: 3
Quotient and remainder
In programming, we combine both these concepts to get the quotient and remainder of some divison:
# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3
print(quotient) # Output: 8
print(remainder) # Output: 2
Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore the concept of floor division in JavaScript. Floor division is a mathematical operation that divides two numbers and returns the largest integer less than or equal to the result. This operation is particularly useful in scenarios where you need to perform integer division and discard any fractional part of the result.
Understanding floor division is essential for various programming tasks, such as implementing algorithms that require integer division, working with loops, and handling array indices.
Before diving into floor division, let's review some fundamental concepts:
Understanding these basics is crucial before moving on to more complex aspects of floor division.
Floor division is a division operation that returns the largest integer less than or equal to the result. In JavaScript, we can achieve floor division using the Math.floor()
function. Let's see how it works:
console.log(Math.floor(3.9)); // Output: 3
console.log(Math.floor(5.2)); // Output: 5
console.log(Math.floor(7)); // Output: 7
We can also use Math.floor()
to compute the integral part of a division:
let a = 3;
let b = 7;
console.log(Math.floor(20 / a)); // Output: 6
console.log(Math.floor(b / a)); // Output: 2
The logical flow behind these concepts is straightforward: perform the division and then apply Math.floor()
to get the largest integer less than or equal to the result.
Let's explore some examples to demonstrate floor division in various contexts:
console.log(Math.floor(10 / 2)); // Output: 5
console.log(Math.floor(15 / 4)); // Output: 3
console.log(Math.floor(20 / 3)); // Output: 6
In real-world scenarios, floor division is useful for tasks such as:
When using floor division, it's important to avoid common mistakes:
Math.floor()
when performing integer division.Best practices for writing clear and efficient code include:
Math.floor()
when you need the integral part of a division.In addition to basic floor division, you can combine it with other techniques to solve more complex problems. For example, you can use floor division along with the modulo operator to calculate both the quotient and remainder:
let dividend = 26;
let divisor = 3;
let quotient = Math.floor(dividend / divisor);
let remainder = dividend % divisor;
console.log(quotient); // Output: 8
console.log(remainder); // Output: 2
Use these advanced techniques when you need to perform more sophisticated calculations involving integer division.
Let's implement a function that performs floor division and returns both the quotient and remainder:
/**
* Performs floor division and returns the quotient and remainder.
* @param {number} dividend - The number to be divided.
* @param {number} divisor - The number by which to divide.
* @returns {Object} An object containing the quotient and remainder.
*/
function floorDivision(dividend, divisor) {
// Calculate the quotient using Math.floor()
let quotient = Math.floor(dividend / divisor);
// Calculate the remainder using the modulo operator
let remainder = dividend % divisor;
// Return an object with the quotient and remainder
return {
quotient: quotient,
remainder: remainder
};
}
// Example usage
let result = floorDivision(26, 3);
console.log(result.quotient); // Output: 8
console.log(result.remainder); // Output: 2
This function demonstrates the correct use of floor division and the modulo operator to obtain both the quotient and remainder.
When debugging code related to floor division, consider the following tips:
Here are some example test cases:
// Test cases for floorDivision function
console.log(floorDivision(10, 2)); // { quotient: 5, remainder: 0 }
console.log(floorDivision(15, 4)); // { quotient: 3, remainder: 3 }
console.log(floorDivision(20, 3)); // { quotient: 6, remainder: 2 }
console.log(floorDivision(26, 3)); // { quotient: 8, remainder: 2 }
When approaching problems related to floor division, consider the following strategies:
In this lesson, we covered the concept of floor division in JavaScript. We explored its significance, fundamental concepts, and practical applications. By mastering floor division, you can perform integer division accurately and efficiently in your programs.
Remember to practice and apply these concepts in various scenarios to strengthen your understanding. Happy coding!
For further reading and practice, consider the following resources: