-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject6_ternary__operators_constructors.html
More file actions
73 lines (60 loc) · 2.3 KB
/
Copy pathProject6_ternary__operators_constructors.html
File metadata and controls
73 lines (60 loc) · 2.3 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project6_ternary_operators_constructors</title>
<script src="JS/main.js"></script>
</head>
<body>
<h2>Voting Eligibility</h2>
<p>Enter your age to see if you can vote:</p>
<input id="Age" value="18" />
<button onclick="Vote_Function()">Check Status</button>
<p id="Vote"></p>
<hr>
<h2>Voter Information (Constructor)</h2>
<button onclick="display_Voter()">Show Voter Details</button>
<p id="Voter_Details"></p>
<hr>
<h2>Nested Function Counter</h2>
<p id="Nested_Function" onclick="count_Function()">Click here to see the nested function result.</p>
</body>
</html>
//main.js
// --- TERNARY OPERATOR WITH INPUT ---
function Vote_Function() {
var Age, Can_vote;
// Get the input value from the browser
Age = document.getElementById("Age").value;
// Ternary operation: checks if age is 18 or older
Can_vote = (Age < 18) ? "You are too young" : "You are old enough";
// Display the result in the HTML element
document.getElementById("Vote").innerHTML = Can_vote + " to vote.";
}
// --- CONSTRUCTOR FUNCTION USING "NEW" AND "THIS" ---
function Voter(Name, Age, Party) {
// "this" keyword creates placeholders for the object's properties
this.Voter_Name = Name;
this.Voter_Age = Age;
this.Voter_Party = Party;
}
// "new" keyword creates unique instances of the Voter object
var Erik = new Voter("Erik", 19, "Independent");
var Sarah = new Voter("Sarah", 22, "Democrat");
// Function to display constructor results in the browser
function display_Voter() {
document.getElementById("Voter_Details").innerHTML =
Erik.Voter_Name + " is " + Erik.Voter_Age + " years old and registered as an " + Erik.Voter_Party + ".";
}
// --- NESTED FUNCTION ---
function count_Function() {
// Calls the inner Count() function and displays the return value
document.getElementById("Nested_Function").innerHTML = Count();
function Count() {
var Starting_number = 17; // A voter's age before their birthday
// Nested function that increments the number
function Plus_one() { Starting_number += 1; }
Plus_one();
return Starting_number; // Returns 18
}
}