-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.js
56 lines (48 loc) · 1.46 KB
/
Calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Simple Calculator Mini Project with User Input
// Function to add two numbers
function add(a, b) {
return a + b;
}
// Function to subtract two numbers
function subtract(a, b) {
return a - b;
}
// Function to multiply two numbers
function multiply(a, b) {
return a * b;
}
// Function to divide two numbers
function divide(a, b) {
if (b !== 0) {
return a / b;
} else {
return 'Cannot divide by zero';
}
}
// Function to perform operations based on user input
function calculate(operator, num1, num2) {
switch (operator) {
case 'add':
return add(num1, num2);
case 'subtract':
return subtract(num1, num2);
case 'multiply':
return multiply(num1, num2);
case 'divide':
return divide(num1, num2);
default:
return 'Invalid operator';
}
}
// Get user input
let operator = prompt('Enter an operator (add, subtract, multiply, divide):');
let number1 = parseFloat(prompt('Enter the first number:'));
let number2 = parseFloat(prompt('Enter the second number:'));
// Check if the input is valid
if (isNaN(number1) || isNaN(number2)) {
console.log('Invalid input. Please enter valid numbers.');
} else {
// Perform the calculation
let result = calculate(operator, number1, number2);
console.log(`Result of ${operator} ${number1} and ${number2} is: ${result}`);
}