0

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
    }
}
Ethan
  • 881
  • 8
  • 14
  • 26
magD
  • 1
  • 2

2 Answers2

0

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

Michael
  • 36
  • 5
  • Unfortunately, it doesn't work, after using alert and eval, I get the whole action. And I want to make the result of the first operation add another operator + a perfect number and get the result – magD Dec 31 '22 at 10:20
0

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.

dkunrath
  • 51
  • 5
  • that wasn't the point. Your code makes it perform the new operation number + operator + second number when it receives the result. And my goal is to get the result of the first operation and the number I get is to become the first number plus the next operator + the next number and get the result. I want the result of the first operation to be the first number of the second operation. I just want to add another operator and another number to it to make it work – magD Dec 31 '22 at 10:18