write calculator operations in javascript in javascript

Here's an example of a basic calculator in JavaScript:

index.tsx
let num1 = parseFloat(prompt("Enter first number: "));
let num2 = parseFloat(prompt("Enter second number: "));
let operator = prompt("Enter operator (+, -, *, /): ");

let result;

if (operator === "+") {
  result = num1 + num2;
} else if (operator === "-") {
  result = num1 - num2;
} else if (operator === "*") {
  result = num1 * num2;
} else if (operator === "/") {
  result = num1 / num2;
} else {
  result = "Invalid operator";
}

console.log(result);
458 chars
20 lines

This prompts the user to enter two numbers and an operator (+, -, *, or /) to perform the operation. Based on the operator entered, the code performs the corresponding calculation and outputs the result to the console.

related categories

gistlibby LogSnag