Calculator Using Class In Javascript






Calculator using Class in JavaScript
\n

\n\n\n\n

\n

Calculator using Class in JavaScript

\n \n

\n \n \n

\n \n

\n \n \n

\n \n \n \n \n

\n Results will appear here\n

\n

\n\n\n\n

Explanation

\n

\n This calculator demonstrates how to use a class in JavaScript to perform basic arithmetic operations.\n The Calculator class takes two numbers as arguments and provides methods for addition,\n subtraction, multiplication, and division.\n

\n

\n When the “Calculate” button is clicked, the input values are retrieved, validated, and then used to\n create an instance of the Calculator class. The results of all operations are then\n displayed in the results section.\n

\n\n

Key Concepts

\n

    \n

  • Class: A blueprint for creating objects. In this case, the Calculator\n class serves as a blueprint for calculator operations.
  • \n

  • Constructor: The Calculator(num1, num2) function acts as the\n constructor, initializing the object with the given numbers.
  • \n

  • Methods: Functions defined within a class that perform operations on the object’s\n data. Here, add, subtract, multiply, and divide\n are methods.
  • \n

\n\n

Practical Examples

\n\n

Example 1: Basic Calculation

\n

Inputs:

\n

    \n

  • Number 1: 10
  • \n

  • Number 2: 5
  • \n

\n

Calculations:

\n

    \n

  • Sum: 10 + 5 = 15
  • \n

  • Difference: 10 – 5 = 5
  • \n

  • Product: 10 × 5 = 50
  • \n

  • Quotient: 10 ÷ 5 = 2
  • \n

\n\n

Example 2: Using Decimal Numbers

\n

Inputs:

\n

    \n

  • Number 1: 12.5
  • \n

  • Number 2: 2.5
  • \n

\n

Calculations:

\n

    \n

  • Sum: 12.5 + 2.5 = 15
  • \n

  • Difference: 12.5 – 2.5 = 10
  • \n

  • Product: 12.5 × 2.5 = 31.25
  • \n

  • Quotient: 12.5 ÷ 2.5 = 5
  • \n

\n\n

Example 3: Handling Division by Zero

\n

Inputs:

\n

    \n

  • Number 1: 8
  • \n

  • Number 2: 0
  • \n

\n

Calculations:

\n

    \n

  • Sum: 8 + 0 = 8
  • \n

  • Difference: 8 – 0 = 8
  • \n

  • Product: 8 × 0 = 0
  • \n

Leave a Reply

Your email address will not be published. Required fields are marked *