-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingSystem.js
More file actions
108 lines (91 loc) · 2.58 KB
/
Copy pathVotingSystem.js
File metadata and controls
108 lines (91 loc) · 2.58 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
104
105
106
107
108
const prompt = require("prompt-sync")();
let candidates = [];
let voters = [];
let votes = {};
let votedVoters = [];
function registerCandidate(){
let name = prompt(" Enter candidate Name:");
let age = parseInt(prompt(" Enter candidate Age:"));
let party = prompt(" Enter candidate political party:");
let relatioshipStatus = prompt(" Enter candidate relatioship status:").toLowerCase();
if(relatioshipStatus == "married" && age >= 18){
candidates.push(name);
votes[name] = 0;
console.log(name, "has been successfuly registered");
} else {
console.log("candidate doesn't meet the requirement");
}
}
function registerVoters(){
let name = prompt(" Enter your Name:");
let age = parseInt(prompt(" Enter your Age:"));
let nationality = prompt("Are you a nigerian:").toLowerCase();
if(age >= 18 && nationality == "yes"){
voters.push(name);
console.log(name, "has been successfuly registered and eligible to vote");
} else {
console.log("you are not eligible");
}
}
function toVote(){
let name = prompt(" Enter your Name:");
if (voters.includes(name) && !votedVoters.includes(name)) {
console.log("Candidates:");
candidates.forEach(candidate => console.log(candidate));
let choice = prompt("Enter the name of the candidate you want to vote for:");
if (candidates.includes(choice)) {
votes[choice]++;
votedVoters.push(name);
console.log("Vote cast successfully");
} else {
console.log("Invalid candidate");
}
} else if (votedVoters.includes(name)) {
console.log("You have already voted.");
} else {
console.log("You are not a registered voter.");
}
}
function countVotes() {
for (let candidate in votes) {
console.log(`${candidate}: ${votes[candidate]} vote(s)`);
}
console.log();
}
function main(){
let hunterly = true
while(hunterly){
console.log("Voting System Menu");
console.log("1.Candidate Resgistration");
console.log("2.Voters Resgistration");
console.log("3.Voting");
console.log("4.count votes");
console.log("0.Exit");
let option = parseInt(prompt("enter an option from (1-6):"));
switch(option){
case 1:
console.log("Candidate Registration");
registerCandidate();
break;
case 2:
console.log("Voters Registration");
registerVoters();
break;
case 3:
console.log("Voting");
toVote();
break;
case 4:
console.log("Counting Votes");
countVotes();
break;
case 0:
console.log("bye bye ");
hunterly = false;
break;
default:
console.log("invalid option. Try again");
}
}
}
main();