1
class Calculator {
  constructor() {
    console.log(`Enter the numbers and the operation index you want to perform: 
        1) Addition 
        2) Substraction 
        3) Multiplication 
        4) Division`);

    this.calculate();
  }

  /** 
   * @param {number} firstValue get the first value
   *  @param {number} secondValue get the second value 
   */
  addition(firstValue, secondValue) {
    return firstValue + secondValue;
  }

  /**
   * @param {number} firstValue get the first value
   *  @param {number} secondValue get the second value 
   */
  subtraction(firstValue, secondValue) {
    return firstValue - secondValue;
  }

  /** 
   *  @param {number} firstValue get the first value 
   *  @param {number} secondValue get the second value 
   */
  multiplication(firstValue, secondValue) {
    return firstValue * secondValue;
  }

  /** 
   *  @param {number} firstValue get the first value 
   *  @param {number} secondValue get the second value 
  */
  division(firstValue, secondValue) {
    if (secondValue != 0) {
      return firstValue / secondValue;
    }
    return 'Cannot perform division by 0';
  }

  calculate() {
    prompt.get(propertiesPrompt, (error, values) => {
      if (error) {
        throw new Error(error);
      }

      console.log(operationResult[values.operation](values.valueOne, values.valueTwo));
      prompt.get(choiceResponse, (responseError, response) => {
        if (responseError) {
          throw new Error(responseError);
        }

        if (response.optionSelected.toLowerCase() == 'y') {
          this.calculate()
        }
      });
    });
  }
}

In the above code I want to remove the parameters in my addition, subtraction, mul and division methods and want to set a variable property in a class, so that I could directly call the stored values in the methods and do the operation. How could that be done?

4 Answers 4

2

Correct name for local class variables is a fields:

class Calculator {
  constructor() {
    this.field = 10
  }

  show() {
    console.log(this.field)
  }
}

Also the name of funcitons of class is a methods

Together methods and fields are members

Sign up to request clarification or add additional context in comments.

2 Comments

This does not solve the problem instead it gives NaN as result.
The problem is in title ow question. The title is How to set local variable in class. So you just need to modify your code self. And now you know how
1

You can set values in constructor

var prompt = require('prompt');

prompt.start();

class Calculator {
  /**
   * @param {number} firstValue get the first value
   *  @param {number} secondValue get the second value
   */
  constructor() {
    console.log(`Enter the numbers and the operation index you want to perform: 
        1) Addition 
        2) Substraction 
        3) Multiplication 
        4) Division`);

    this.firstValue = 0;
    this.secondValue = 0;

    this.calculate();
  }

  addition() {
    return this.firstValue + this.secondValue;
  }

  subtraction() {
    return this.firstValue - this.secondValue;
  }

  multiplication() {
    return this.firstValue * this.secondValue;
  }

  division() {
    if (this.secondValue != 0) {
      return this.firstValue / this.secondValue;
    }
    return 'Cannot perform division by 0';
  }

  calculate() {
    prompt.get(['firstValue', 'secondValue'], (error, values) => {
      if (error) {
        throw new Error(error);
      }

      this.firstValue = Number(values.firstValue);
      this.secondValue = Number(values.secondValue);

      prompt.get(['choice'], (responseError, response) => {
        if (responseError) {
          throw new Error(responseError);
        }

        let result = 0;

        switch (Number(response.choice)) {
          case 1:
            result = this.addition();
            break;
          case 2:
            result = this.subtraction();
            break;
          case 3:
            result = this.multiplication();
            break;
          case 4:
            result = this.division();
            break;
          default:
            console.log('>>>>>  Please select valid operation');
            break;
        }

        console.log('>>>>>  result : ', result);

        prompt.get(['optionSelected'], (responseError, response) => {
          if (responseError) {
            throw new Error(responseError);
          }

          if (response.optionSelected.toLowerCase() == 'y') {
            this.calculate();
          }
        });
      });
    });
  }
}

new Calculator();

5 Comments

The result it is showing is NaN, not giving a proper result
@user6819864 how did you checked ?
The thing is in your code, you had put hard coded values, while I want to get the values from users. When taking the values from users in calculate() the values are not been properly stored in the variables.
Enter the numbers and the operation index you want to perform: 1) Addition 2) Substraction 3) Multiplication 4) Division prompt: Enter the first value: 55 prompt: Enter the second value: 55 prompt: Enter the operation index.: 1 NaN prompt: Do you want to again run the calculator app type 'y' or 'n': Here Nana is coming as a result
New to stack, reputation is low, Sorry can't
0

You can set a local variable in a class using the keyword this. Just set it by using this.myVar = myValue anywhere in your class. The variable does not have to be defined before you assign it and can be reached anywhere in your class in the same way. Note that it is also accessible (and writeable) from outside the class.

2 Comments

Could you kindly add this in my code and show it, bcz its not working either. Everytime it gives undefined
I don't think you can use "this" reference within the class space. The "this" must be used only within a method in the class. Two ways: 1) create the variables in the class space (outside of any methods) and then you can access them within any method via the "this" reference. 2) Alternatively, you can set them within the constructor using the "this" reference.
0

Classes is not a functions which have a local scope or global scope. Classes have fields and you can use your fields as follow:

class Calculator {
    constructor() { 
         this.field = 10
    } 
    print() { 
        console.log(this.field)
     }
 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.