-1

I'm trying to make a simple calculator. As of right now though, the code only executes the first line, meaning it only divides. Sometimes it'll run all four ifs at the same time, even if you only enter the '/' operand or the '+' operand. I've tried using else if but it doesn't seem to work.

<html>
<head>Js Assignment</head>
<body>
    <script type="text/javascript">
        let firstNumber = prompt('enter the first number');
        let operand = prompt('enter the operand');
        let secondNumber = prompt('enter the second number');
        
        if (operand = '/') {
            alert(firstNumber/secondNumber);
        }
        if (operand = '*') {
            alert(firstNumber*secondNumber);
        }
        if (operand = '+') {
            alert(firstNumber+secondNumber);
        }
        if (operand = '-') {
            alert(firstNumber-secondNumber);
        }
    </script>
</body>
breezy
  • 11
  • 2

1 Answers1

0

u have to use == to compare

<html>
<head>Js Assignment</head>
<body>
    <script type="text/javascript">
        let firstNumber = prompt('enter the first number');
        let operand = prompt('enter the operand');
        let secondNumber = prompt('enter the second number');
        
        if (operand == '/') {
            alert(firstNumber/secondNumber);
        }
        if (operand == '*') {
            alert(firstNumber*secondNumber);
        }
        if (operand == '+') {
            alert(firstNumber+secondNumber);
        }
        if (operand == '-') {
            alert(firstNumber-secondNumber);
        }
    </script>
</body>
ashen madusanka
  • 354
  • 3
  • 11