DEV Community

King coder
King coder

Posted on

Practical JavaScript Math Problems to Master Before DSA

1. Round Up Prices

  • Given an array of prices: [12.1, 5.3, 8.9]
  • Return a new array with each price rounded up to the nearest whole number

Example Output:

[13, 6, 9]
Enter fullscreen mode Exit fullscreen mode

Code

let numbers = [12.1, 5.3, 8.9];

for(let i = 0; i < numbers.length; i++) {
  numbers[i] = Math.round(numbers[i]);
}

console.log(numbers);


Enter fullscreen mode Exit fullscreen mode

2. Round Down Student Scores

Given scores: [87.6, 92.4, 78.9, 65.3]

Return scores rounded down to whole numbers.

Example Output:

[87, 92, 78, 65]
Enter fullscreen mode Exit fullscreen mode

Code


let numbers = [87.6, 92.4, 78.9, 65.3]

for(let i = 0; i < numbers.length; i++) {
  numbers[i] = Math.floor(numbers[i]);
}

console.log(numbers);

Enter fullscreen mode Exit fullscreen mode

3. Round to Nearest Rating

Ratings: [4.5, 3.2, 4.8, 2.4]

Round each rating to the nearest whole number.

Example Output:

[5, 3, 5, 2]
Enter fullscreen mode Exit fullscreen mode

Code

let numbers = [4.5, 3.2, 4.8, 2.4]

for(let i = 0; i < numbers.length; i++) {
  numbers[i] = Math.round(numbers[i]);
}

console.log(numbers);

Enter fullscreen mode Exit fullscreen mode

4. Total Income (Ignoring Decimals)

Monthly incomes: [1200.50, 899.99, 1500.20]

Calculate total income by ignoring decimal parts.

Example Output:

3599 (1200 + 899 + 1500)
Enter fullscreen mode Exit fullscreen mode

Code

let numbers = [1200.50, 899.99, 1500.20]
let sum = 0

for(let i = 0; i < numbers.length; i++) {
  sum += Math.floor(numbers[i]);

}

console.log(sum); 
Enter fullscreen mode Exit fullscreen mode

5. Dice Roll Simulator

Write a function that returns a random number between 1 and 6 inclusive, simulating a dice roll.


Code

console.log(Math.floor(Math.random() * 6 + 1)); 
Enter fullscreen mode Exit fullscreen mode

6. Sum Integer Parts Only

Numbers: [1.5, 87.5, 47.8]

Sum only the integer parts, ignoring decimals.

Example Output:

135 (1 + 87 + 47)
Enter fullscreen mode Exit fullscreen mode

Code


let numbers = [1.5, 87.5, 47.8]
let sum = 0

for(let i = 0; i < numbers.length; i++) {
  sum += Math.floor(numbers[i]);

}

console.log(sum); // Output: 135


Enter fullscreen mode Exit fullscreen mode

7. Highest and Lowest Temperature

Temperatures: [21.3, 29.4, 19.7, 25.5, 31.2]

Return both the highest and lowest temperature.

Example Output:

Highest: 31.2, Lowest: 19.7
Enter fullscreen mode Exit fullscreen mode

Code


let numbers = [21.3, 29.4, 19.7, 25.5, 31.2]


console.log('Lowest Temperature :-', Math.min(...numbers)); // Output: 19.7 
console.log('Highest Temperature :-', Math.max(...numbers)); // Output: 31.2


Enter fullscreen mode Exit fullscreen mode

8. Apply Discount and Round

Prices: [100, 200, 150]

Apply a 20% discount and round final prices to nearest whole number.

Example Output:

[80, 160, 120]
Enter fullscreen mode Exit fullscreen mode

9. Absolute Difference Function

Create a function that takes two numbers and returns their absolute difference.

Example:

difference(10, 15) ➞ 5
Enter fullscreen mode Exit fullscreen mode

10. Square Roots of Numbers

Numbers: [4, 9, 16, 25]

Return an array of their square roots.

Example Output:

[2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Code


let numbers = [4, 9, 16, 25]

let squareRoots = numbers.map(Math.sqrt)

console.log(squareRoots) // Output: [2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

11. Power Function

Write a function that takes base and exponent and returns base raised to exponent.

Example:

power(2, 3) ➞ 8
Enter fullscreen mode Exit fullscreen mode

Code



function power(base, exponent) {
    return Math.pow(base, exponent);
}   

console.log(power(2, 3)); // 8

Enter fullscreen mode Exit fullscreen mode

12. Pick Random Character

Characters: ["a", "b", "c", "d", "e"]

Return a random character from the array.


13. Round Up Weights

Weights: [2.45, 1.75, 3.6, 2.9]

Round each weight up to the nearest whole number.

Example Output:

[3, 2, 4, 3]
Enter fullscreen mode Exit fullscreen mode

14. Simulate 100 Coin Tosses

Simulate tossing a coin 100 times. Count how many times you get heads and tails.


15. Total User Scores (Ignore Decimals)

Scores: [23.6, 25.9, 24.1, 22.8]

Sum integer parts only.

Example Output:

94
Enter fullscreen mode Exit fullscreen mode

16. Calculate Areas of Circles

Radii: [3, 5, 7]

Return an array of areas (πr²) rounded to 2 decimals.

Example Output:

[28.27, 78.54, 153.94]`
Enter fullscreen mode Exit fullscreen mode

17. Generate Random Password

Characters: ["a", "b", "c", "d", "e", "1", "2", "3"]

Generate a 5-character random password.


18. Difference Between Max and Min

Numbers: [10, 25, 7, 30, 18]

Return the difference between max and min numbers.

Example Output:

23
Enter fullscreen mode Exit fullscreen mode

19. Round Down Ages

Ages: [18.9, 21.5, 16.7, 25.3]

Return array with ages rounded down.

Example Output: [18, 21, 16, 25]


20. Cube of Numbers

Numbers: [2, 3, 4]

Return their cubes.

Example Output: [8, 27, 64]


💡 Pro Tip

Try to solve these problems using JavaScript’s built-in Math methods such as:

Math.ceil(), Math.floor(), Math.round(), Math.trunc(), Math.random(), Math.max(), Math.min(), Math.pow(), Math.sqrt(), Math.abs(), and Math.PI.


Ready to level up your JavaScript math skills?

Start coding now and watch your confidence grow! 💻🔥


Feel free to share your solutions or ask questions in the comments!

Top comments (0)