Home > database >  JavaScript adding additional actions after receiving the result
JavaScript adding additional actions after receiving the result

Time:12-31

I'm making a calculator in JS that displays commands and the result in a prompt.

I want to add an action that, after receiving the result of the operation, makes it possible to multiply, divide, etc. by another number.

Action plan: First action... = result > choose selector ( , -, *,/) > choose second number > result --- and so on until you stop pressing enter with a blank field.

I was thinking of starting with a while loop to add the ability to choose a character but I don't know if it will affect the result of the previous action, additionally I don't know how to add the ability to choose the next number

    switch (operator) {
        case ' ':
            alert(numb1   numb2)
            break
        case '-':
            alert(numb1 - numb2)
            break
        case '*':
            alert(numb1 * numb2)
            break
        case '/':
            alert(numb1 / numb2)
            break
        case '%':
            alert(numb1 % numb2)
            break
    }
    while (true) {
        let result =  prompt('Enter an arithmetic operator or leave blank.')

        if (!result) break
    }
}

CodePudding user response:

Yes you can use while loop with a new param to get the new value input with prompt, such that:

    while (true) {
        let result =  prompt('Enter an arithmetic operator or leave blank.')

        if (!result) break
        let newNumber =  prompt('Enter a new number')
        // do the arithmetic operation
    }

Additionally, the operation in switch seems to be quite redundant. i.e.: one case for alert in one character / operator change. You might want to use eval() for the operation.

    let numb1 = 1 // original number
    while (true) {
        let result = prompt('Enter an arithmetic operator or leave blank.')

        if (!result) break
        let newNumber = prompt('Enter a new number')
        // do the arithmetic operation
        // if numb1 = 1, new Number = 2, result = ' ', it will show '1   2'
        alert(`${numb1} ${result} ${newNumber}`)

        // numb1 = 3
        numb1 = eval(`${numb1} ${result} ${newNumber}`) 


    }

For converting string to operation, please refer to this answer: https://stackoverflow.com/a/26551015/9067107

CodePudding user response:

This seens like a learning exercise, so I'll try to explain some thought before showing some code.

Your switch case assumes num1 and num2 are already known, but what you described says that num2 will only come after the operator. Also, you want that math being done inside the loop, else it will only run once.

What you want is:

  • type first number;
  • type operator;
  • type next number;
  • show result = first/old number (operator) next/last number input;
  • type operator;
  • type next number;
  • show result = first/old number (operator) next/last number input;

... and so on until the "end command" is input.

We have to respect that order, so we'll need to store results and actions. This way we can keep the loop "alive".

Another thing, to be a "good loop", it has to start and end with the same actions. So, we'll leave the first number input out of the loop, that way it will be something liek this:

  1. ask "first number"
  2. ask "operator" -> (end?) -> ask "next number" -> store and show "result"
  3. ask "operator" -> (end?) -> ask "next number" -> store and show "result"

... and so on ...

// start prompting the user for the first number.
let oldNum =  prompt("first number: ");
let continue = true; // this is the condition to continue or end our loop.

while(continue) {
  // prompt the user for the operator
  let op = prompt('operator: ');
  if (!(op === " " || op === "-" || op === "/" || op === "*")) break;
  // if anything aside from the expected inputs, the loop breaks.

  let num = prompt('next number: ');
  switch(op) {
    case ' ':
      oldNum  = num; // we add the new value to the old one.
      break;
    case '-':
      oldNum -= num;
      break;
    case '/':
      oldNum /= num;
      break;
    case '*':
      oldNum *= num;
      break;
  }
  // show result to user
  alert(oldNum);
}

BTW, there are better ways to write this particular code, tried to make it similar to what you shown.

  • Related