Day 99: Advanced Math Operations | 100 Days of JavaScript Challenge 🧮
HTML-код
- Опубликовано: 6 фев 2025
- 🎯 Welcome to Day 99 of the 100 Days of JavaScript Coding Challenge! 🚀
#CodeWithPoonam #100DaysOfCode
📝 Problem Statement:
Write a class called MathOperations that has:
A property number to store the number.
Methods:
getSquare() - Calculates and logs the square of the number.
getCube() - Calculates and logs the cube of the number.
getFactorial() - Calculates and logs the factorial of the number.
📊 Example Usage:
let math = new MathOperations(5);
math.getSquare(); // Output: The square of 5: 25
math.getCube(); // Output: The cube of 5: 125
math.getFactorial(); // Output: The factorial of 5: 120
let math1 = new MathOperations(7);
math1.getSquare(); // Output: The square of 7: 49
math1.getCube(); // Output: The cube of 7: 343
math1.getFactorial(); // Output: The factorial of 7: 5040
💡 Approach:
Use a class MathOperations with a constructor to initialize the number.
Implement:
Square using ** or Math.pow().
Cube using ** or Math.pow().
Factorial using a for loop.
Log the outputs clearly for each calculation.
🚀 Challenge Yourself Further:
Add a getPower(n) method to calculate number^n.
Example: math.getPower(4) → 5^4 = 625
Include error handling for invalid inputs (e.g., negative numbers for factorial).
Allow chaining operations: Example:
let result = math.getSquare().getCube();
🌟 Day 99: You're almost at the finish line! Keep coding and celebrating your progress. 💪
#javascriptcoding #100DaysOfCode2024 #codingchallenges #oopinjavascript