The abs()
function returns the absolute value of a number. This means that it returns a number's positive value. You can think of it as the distance away from zero.
Create your own function abs()
that recreates this functionality.
Examples:
abs(-1.217197940) ➞ 1.21719794 abs(-544.0) ➞ 544 abs(250.3) ➞ 250.3 abs(0) ➞ 0 abs(-0.14) ➞ 0.14
Note:
Do not use the abs()
function provided by JavaScript (it will defeat the purpose of the challenge)
The core challenge of this problem is to determine the absolute value of a number without using the built-in abs()
function. The absolute value of a number is its non-negative value, regardless of its sign. This function is commonly used in mathematical computations, data analysis, and various algorithms where distance or magnitude is considered.
Potential pitfalls include handling edge cases such as zero and negative zero, and ensuring the function works for both integers and floating-point numbers.
To solve this problem, we need to check if the number is negative. If it is, we return its positive counterpart; otherwise, we return the number as is.
Let's start with a naive solution:
function abs(n) {
// Check if the number is negative
if (n < 0) {
// Return the positive counterpart
return -n;
} else {
// Return the number as is
return n;
}
}
This solution is straightforward and works in constant time, O(1), since it only involves a simple conditional check and possibly a negation operation.
Here is a step-by-step breakdown of the algorithm:
n
is less than zero.n
is negative, return -n
.n
is non-negative, return n
.Below is the JavaScript implementation of the abs()
function:
function abs(n) {
// Check if the number is negative
if (n < 0) {
// Return the positive counterpart
return -n;
} else {
// Return the number as is
return n;
}
}
// Test cases
console.log(abs(-1.217197940)); // ➞ 1.21719794
console.log(abs(-544.0)); // ➞ 544
console.log(abs(250.3)); // ➞ 250.3
console.log(abs(0)); // ➞ 0
console.log(abs(-0.14)); // ➞ 0.14
The time complexity of this solution is O(1) because it involves a single conditional check and possibly a negation operation, both of which are constant-time operations.
The space complexity is also O(1) as we are not using any additional data structures that grow with the input size.
Potential edge cases include:
n = 0
: The function should return 0.n = -0
: The function should return 0.Examples:
console.log(abs(0)); // ➞ 0
console.log(abs(-0)); // ➞ 0
console.log(abs(-1e10)); // ➞ 10000000000
console.log(abs(1e10)); // ➞ 10000000000
To test the solution comprehensively, we should include a variety of test cases:
Example test cases:
console.log(abs(123)); // ➞ 123
console.log(abs(-123)); // ➞ 123
console.log(abs(0)); // ➞ 0
console.log(abs(-0)); // ➞ 0
console.log(abs(1.23)); // ➞ 1.23
console.log(abs(-1.23)); // ➞ 1.23
console.log(abs(1e10)); // ➞ 10000000000
console.log(abs(-1e10)); // ➞ 10000000000
When approaching such problems, consider the following tips:
In this blog post, we discussed how to recreate the abs()
function in JavaScript. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills and improving your coding abilities.
We encourage you to practice and explore further to deepen your understanding.
For further reading and practice problems, consider the following resources: