-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6. Function.html
More file actions
63 lines (51 loc) · 1.6 KB
/
Copy path6. Function.html
File metadata and controls
63 lines (51 loc) · 1.6 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
<!DOCTYPE html>
<html>
<head>
<title>JS Function Basics</title>
<style>
body {
font-family: monospace;
padding: 20px;
}
.box {
border: 1px solid #000;
padding: 15px;
margin-bottom: 10px;
}
input {
width: 50px;
}
</style>
</head>
<body>
<h3>1. Input Values</h3>
<div class="box">
<input id="num1" type="number" value="10"> +
<input id="num2" type="number" value="5">
<button onclick="runLogic()">Run Function</button>
</div>
<h3>2. Output</h3>
<div class="box" id="resultDisplay">
Click the button to see the math.
</div>
<script>
// --- THE ENGINE (The Function) ---
// This takes two "parameters" (a and b), processes them, and returns the result.
function addNumbers(a, b) {
return a + b;
}
// --- THE CONTROLLER ---
// This grabs the data from the screen and passes it to the engine.
function runLogic() {
// 1. Get values from inputs
let x = Number(document.getElementById("num1").value);
let y = Number(document.getElementById("num2").value);
// 2. Call the function and store the "return" value
let sum = addNumbers(x, y);
// 3. Display the result
document.getElementById("resultDisplay").innerHTML =
"The function returned: <b>" + sum + "</b>";
}
</script>
</body>
</html>