The else is not recognized

-2

alert("");
            var n1=parseInt(prompt("Digite um número"));
            var n2=parseInt(prompt("Digite outro número"));
            var A = n1+n2;
            var M = n1*n2;
            var D = n1/n2;
            var Sub = n1-n2;

            var menu=prompt("Escolha uma opção:\nA - Adição\nM - Multiplicação\nD - Divisão \nS - Subtração")

            if(menu == A);{
                alert(A);
            }else(menu == M);{
                alert(M);
            }
    
asked by anonymous 13.05.2018 / 03:58

2 answers

1

There are some problems with your code:

  • There is no semicolon ( ; ) after if and else ;
  • As you are trying to compare with the value of the prompt entered by the user, the letter A would be a String, so it should be enclosed in quotation marks, else the javascript will look for a variable with the name A, unlike the letter itself A.
  • In the case of else there is no condition inside. Think of else as but all other options . If you want to set a condition, you could use else if , but in your case it is not necessary.
  • This is a simple example of how to do this:

    var n1 = parseInt(prompt("Digite um número"));
    var n2 = parseInt(prompt("Digite outro número"));
    var A = n1 + n2;
    var M = n1 * n2;
    var D = n1 / n2;
    var Sub = n1 - n2;
    
    var menu=prompt("Escolha uma opção:\nA - Adição\nM - Multiplicação\nD - Divisão \nS - Subtração")
    
    if (menu === 'A') {
      alert(A);
    } else if (menu === 'M') {
      alert(M);
    } else if (menu === 'D') {
      alert(D);
    } else {
      alert(Sub);
    }
        
    13.05.2018 / 04:18
    3

    The ideal would be to learn in a structured way, to understand the reason for each thing.

    Can not have a semicolon after the condition of if or else . The ; encloses a declaration, and the whole block of if is a unique statement.

    In addition, when you put a condition in else it should be followed by if .

    When you compare a variable with a text, the text must be enclosed in quotation marks, otherwise it is interpreted as a variable.

    There are a number of other things that can be improved in this code.

    var n1 = parseInt(prompt("Digite um número"));
    var n2 = parseInt(prompt("Digite outro número"));
    var A = n1 + n2;
    var M = n1 * n2;
    var D = n1 / n2;
    var Sub = n1 - n2;
    var menu = prompt("Escolha uma opção:\nA - Adição\nM - Multiplicação\nD - Divisão \nS - Subtração")
    if (menu == "A") {
        alert(A);
    } else if (menu == "M") {
        alert(M);
    }
        
    13.05.2018 / 04:18