-1

I'm trying to create a calculator program for my assignment course in JavaScript. The program has to calculate to numbers (values assigned by the user), as well as the operator (+, - , * ,/), and to display the result. If the user input an extra character in the field like (15n + 20 or 15 ++ 20 or 15+ + 20) has to display an error "Cannot concatenate a number with a string"

if the user divide a number by 0 has to display "Division by 0 is not allowed" Please I really need your help. I posted, my code bellow, let me know. What I'm doing wrong? I want to mention that I'm at the beginning level. Thanks, I appreciate all the advices

    let numberOne = parseFloat(prompt("First Number: "));
    let operator = prompt("Chose operation (+ - * /): ");
    let numberTwo = parseFloat(prompt("Second Number: " ));


    function DivisionByZero(a, b) {
    
      if (a == 0 || b == 0 == true) {
    
        throw "Divizion by 0 is not allowed";
       
      }
    
    }
    
    
    function TypingError(a, b, c) {
    
      if (numberOne  +  " " == true) {
    
        throw "Cannot concatenate a number with a string";
    
        
      } else if (numberTwo  + " " == true) {
    
        throw "Cannot concatenate a number with a string";
    
      } else if (operator + operator == true) {
    
        throw "Can not concatenate two operators"
    
      } 
    }
    
    let TypingErrorDisplay = TypingError(numberOne, operator, numberTwo);
    
    
    
    
    var ErrorDivisionDisplay = DivisionByZero(numberOne, numberTwo);
    
    if (operator === "+") {
      var calc = parseFloat(numberOne) + parseFloat(numberTwo);
      alert(calc);
      
    
    } 
    
    else if (operator === "-") {
      var calc = parseFloat(numberOne) - parseFloat(numberTwo);
      alert(calc);
    
     
    } 
    
    else if (operator === "*") {
      var calc = parseFloat(numberOne) * parseFloat(numberTwo);
      alert(calc);
    
    } 
    
    else if (operator === "/") {
      var calc = parseFloat(numberOne) / parseFloat(numberTwo);
      alert(calc);
    
      
      if (ErrorDivisionDisplay == true) {
    
        alert("Division by 0 is not allowed");
    
      }
    } 
    
    else {
      alert(TypingErrorDisplay);
    }
BOZ
  • 2,731
  • 11
  • 28
  • A few quick tips ```parseFloat``` will return NaN if there is something other than a digit/decimal string. In your divisionbyzero function the if statement doesn’t need to == true it will evaluate to a Boolean without it. – niles87 Apr 05 '21 at 09:29
  • You have an issue with you test of division by 0. You check if value is not zero _AFTER_ actually do division. Move your test `if (ErrorDivisionDisplay == true) {...}` **before** the calculation `var calc = parseFloat(numberOne) / parseFloat(numberTwo);` – Camille Apr 05 '21 at 09:32
  • 1
    And could be better if you write what's you problem. Error message? Wanted logic not applied? ... – Camille Apr 05 '21 at 09:34
  • [Non Empty Strings](https://stackoverflow.com/questions/4923631/why-does-ifstring-evaluate-string-as-true-but-if-string-true-does-not) will equate to `true`. In these checks `if (numberOne + " " == true) {` you are making a string out of *numberOne* by adding an empty string to it, which will to equate to `true` when checked with double equal signs. Try looking up the difference between `==` and `===`. – Alain Doe Apr 05 '21 at 09:34
  • Please read https://stackoverflow.com/help/how-to-ask for information on how to write your question better so that others are able to help. – CWSites Apr 05 '21 at 12:18

3 Answers3

0

First of all, in your first condition you have

function DivisionByZero(a, b) {

  if (a == 0 || b == 0 == true) {

    throw "Divizion by 0 is not allowed";
   
  }

}

You can't have b == 0 == true. You just need :

function DivisionByZero(a, b) {

  if (a === 0 || b === 0) {

    throw "Divizion by 0 is not allowed";
   
  }

}

for the rest, you should check that post : Check if a variable is a string in JavaScript

function TypingError(a, b, c) {

  if (typeOf(numberOne) == "string") {

    throw "Cannot concatenate a number with a string";
  }

    
  else if (typeOf(numberTwo) === "string") {

    throw "Cannot concatenate a number with a string";

  } 
  else if (operator.lenght > 1) {

    throw "Can not concatenate two operators"

  } 
}

Try to correct that things fisrt and tell us.

Axel,

Axel W
  • 63
  • 7
0
let numberOne = parseFloat(prompt("First Number: "));
let operator = prompt("Chose operation (+ - * /): ");
let numberTwo = parseFloat(prompt("Second Number: " ));


function DivisionByZero(a, b) {

  if (a == 0 || b == 0) {  // You don't need == true, you already declared the conditions 

    throw "Divizion by 0 is not allowed";
   
  }

}


function TypingError(n1, op, n2) {

  if (!n1.isFinite()) { // Triggers if numberOne is not finite

    throw "Cannot concatenate a number with a string";

    
  } else if (!n2.isFinite()) { // Triggers if numberTwo is not finite

    throw "Cannot concatenate a number with a string";

  } else if (op != "+" && op != "-" && op != "*" && op != "/") { // Accept only valid arithmetic operators

    throw "Can not concatenate two operators"

  } 
}

let TypingErrorDisplay = TypingError(numberOne, operator, numberTwo);

var ErrorDivisionDisplay = DivisionByZero(numberOne, numberTwo);

if (operator === "+") {
  var calc = parseFloat(numberOne) + parseFloat(numberTwo);
  alert(calc);  
} 

else if (operator === "-") {
  var calc = parseFloat(numberOne) - parseFloat(numberTwo);
  alert(calc);

 
} 

else if (operator === "*") {
  var calc = parseFloat(numberOne) * parseFloat(numberTwo);
  alert(calc);

} 

else if (operator === "/") {
  var calc = parseFloat(numberOne) / parseFloat(numberTwo);
  alert(calc);

  
  if (ErrorDivisionDisplay == true) {

    alert("Division by 0 is not allowed");

  }
} 

else {
  alert(TypingErrorDisplay);
}
0

#Solution#

The solution that I found it is displayed below. Please let me know what you think about it.

let numberOne = prompt("First Number: ");
let operator = prompt("Chose operation (+ - * /): ");
let numberTwo = prompt("Second Number: ");


function TypingError(op1, op2, operation) {

  if (isNaN(numberOne) || numberOne.length == 0 || isNaN(numberTwo) || numberTwo.length == 0) {
    return -1;
  }

  if (operation !== undefined && operation.length > 1) {
    return -2;

  }
  return 0;
}


let TypingErrorDisplay = TypingError(numberOne, numberTwo, operator);

if (TypingErrorDisplay == -1) {
  alert("one of the operands is not numeric ");
}
else if (TypingErrorDisplay == -2) {
  alert("wrong operation - symbol must have one character");
}
else {
  numberOne = parseFloat(numberOne);
  numberTwo = parseFloat(numberTwo);
  if (operator == "+") {

    var calc = numberOne + numberTwo;
    alert(calc);
  }

  else if (operator == "-") {
    var calc = numberOne - numberTwo;
    alert(calc);

  }

  else if (operator == "*") {
    var calc = numberOne * numberTwo;
    alert(calc);
  }

  else if (operator === "/") {
    if (numberTwo == 0) {
      alert("Division by 0 is not allowed");
    }
    else {
      var calc = numberOne / numberTwo;;
      alert(calc);
    }
  }
  else {
    alert("operation " + operator + " not exists");
  }
}