-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.2-built-in.html
More file actions
103 lines (86 loc) · 3.41 KB
/
04.2-built-in.html
File metadata and controls
103 lines (86 loc) · 3.41 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Built-in Functions | Playground</title>
<script>
/*
---------------------------------------------------------
JavaScript Assignment
---------------------------------------------------------
TASK:
-----
1. This simple program asks the user to enter two numbers.
2. It multiplies the numbers and displays the result
directly on the webpage.
STUDENT ASSIGNMENT:
--------------------
Using this basic example, create a full calculator that
can perform ALL of the following operations:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%)
REQUIREMENTS:
--------------
1. Ask the user to enter two values.
2. Ask the user to choose which operation they want to perform.
3. Use if/else to perform the operation.
4. Display the final result on the page using document.write()
or (preferably) using HTML elements + innerHTML.
5. Make sure your calculator handles invalid input properly.
---------------------------------------------------------
*/
let numone = parseInt(prompt("First Value"));
let operator = prompt("Choose Operator in (+ - * / %)");
let numtwo = parseInt(prompt("Second Value"));
let result;
if (!isNaN(numone) || !isNaN(numtwo)) {
if (operator == "+") {
result = numone + numtwo;
document.write(numone + " " + operator + " " + numtwo + " = " + result);
}
else if (operator == "-") {
result = numone - numtwo;
document.write(numone + " " + operator + " " + numtwo + " = " + result);
}
else if (operator == "*") {
result = numone * numtwo;
document.write(numone + " x " + numtwo + " = " + result);
}
else if (operator == "/") {
if (numtwo == 0) {
result = `Division by ${numtwo} is not allowed.`;
document.write(result);
}
else {
result = numone / numtwo;
document.write(numone + " " + operator + " " + numtwo + " = " + result);
}
}
else if (operator == "%") {
result = numone % numtwo;
document.write(numone + " " + operator + " " + numtwo + " = " + result);
}
// ! = = -> Strict Value or Type
else {
result = `"${operator}" isn't a valid operator, Please enter valid operator.`;
document.write(result);
}
}
else if (numone.length == 0 || numtwo.length == 0 || operator.length == 0) {
result = "We can't proceed with empty fields.";
document.write(result);
}
else {
result = "For calculation numeric values required.";
document.write(result);
}
</script>
</head>
<body>
<!-- ... empty ... -->
</body>
</html>