Tally votes and determine an election winner. Involves looping through nested objects and their properties.
//In this challenge you will work with the following JavaScript objects.
//Do not alter these objects here.
// These are the votes cast by each student.
var votes = {
"Alex": { president: "Bob", vicePresident: "Devin", secretary: "Gail", treasurer: "Kerry" },
"Bob": { president: "Mary", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
"Cindy": { president: "Cindy", vicePresident: "Hermann", secretary: "Bob", treasurer: "Bob" },
"Devin": { president: "Louise", vicePresident: "John", secretary: "Bob", treasurer: "Fred" },
"Ernest": { president: "Fred", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
"Fred": { president: "Louise", vicePresident: "Alex", secretary: "Ivy", treasurer: "Ivy" },
"Gail": { president: "Fred", vicePresident: "Alex", secretary: "Ivy", treasurer: "Bob" },
"Hermann": { president: "Ivy", vicePresident: "Kerry", secretary: "Fred", treasurer: "Ivy" },
"Ivy": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Gail" },
"John": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Kerry" },
"Kerry": { president: "Fred", vicePresident: "Mary", secretary: "Fred", treasurer: "Ivy" },
"Louise": { president: "Nate", vicePresident: "Alex", secretary: "Mary", treasurer: "Ivy" },
"Mary": { president: "Louise", vicePresident: "Oscar", secretary: "Nate", treasurer: "Ivy" },
"Nate": { president: "Oscar", vicePresident: "Hermann", secretary: "Fred", treasurer: "Tracy" },
"Oscar": { president: "Paulina", vicePresident: "Nate", secretary: "Fred", treasurer: "Ivy" },
"Paulina": { president: "Louise", vicePresident: "Bob", secretary: "Devin", treasurer: "Ivy" },
"Quintin": { president: "Fred", vicePresident: "Hermann", secretary: "Fred", treasurer: "Bob" },
"Romanda": { president: "Louise", vicePresident: "Steve", secretary: "Fred", treasurer: "Ivy" },
"Steve": { president: "Tracy", vicePresident: "Kerry", secretary: "Oscar", treasurer: "Xavier" },
"Tracy": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
"Ullyses": { president: "Louise", vicePresident: "Hermann", secretary: "Ivy", treasurer: "Bob" },
"Valorie": { president: "Wesley", vicePresident: "Bob", secretary: "Alex", treasurer: "Ivy" },
"Wesley": { president: "Bob", vicePresident: "Yvonne", secretary: "Valorie", treasurer: "Ivy" },
"Xavier": { president: "Steve", vicePresident: "Hermann", secretary: "Fred", treasurer: "Ivy" },
"Yvonne": { president: "Bob", vicePresident: "Zane", secretary: "Fred", treasurer: "Hermann" },
"Zane": { president: "Louise", vicePresident: "Hermann", secretary: "Fred", treasurer: "Mary" }
};
// Tally the votes in voteCount.
var voteCount = {
president: {},
vicePresident: {},
secretary: {},
treasurer: {}
};
/* The name of each student receiving a vote for an office should become a property
of the respective office in voteCount. Once the votes have been tallied, assign
each officer position the name of the student who received the most votes. */
var officers = {
president: undefined,
vicePresident: undefined,
secretary: undefined,
treasurer: undefined
};
/*Below you will find driver code. Run the code in this file either
from the command line using Node.js or by pasting the code of this
entire file into your browser console. All tests will log true
in the console when they pass--false, otherwise.*/
// __________________________________________
// Write your code below.
var offices = ["president", "vicePresident", "secretary", "treasurer"];
var tallyVotes = function() {
for (var voter in votes) {
for (var i=0; i<offices.length; i++) {
var position = offices[i];
if ((voteCount[position]).hasOwnProperty(votes[voter][position])) {
voteCount[position][votes[voter][position]]++;
}
else {
voteCount[position][votes[voter][position]] = 1;
}
}
}
};
tallyVotes();
var declareWinner = function() {
for (var office in voteCount){
var scores = voteCount[office];
var record = 0;
var winner;
for (var name in scores) {
if ((scores[name]) > record) {
record = scores[name];
winner = name;
}
}
officers[office] = winner;
}
};
declareWinner();
/*Refactor and Reflect
This challenged really helped me gain more confidance with javascript and specifically syntax and for/for..in loops;
That said, although I tried to refactor, I don't know how DRY or good my code is--there just seems to be so much to
learn in JS and it seems overwhelming to tackle, especially because I don't know exactly what to focus on for our purposes.
Having the bit of background that I have in Java does help a little the the syntax and loop logic.
*/
// __________________________________________
// Driver Code: Do not alter code below this line.
function assert(test, message, test_number) {
if (!test) {
console.log(test_number + "false");
throw "ERROR: " + message;
}
console.log(test_number + "true");
return true;
}
assert(
(voteCount.president["Bob"] === 3),
"Bob should receive three votes for President.",
"1. "
);
assert(
(voteCount.vicePresident["Bob"] === 2),
"Bob should receive two votes for Vice President.",
"2. "
);
assert(
(voteCount.secretary["Bob"] === 2),
"Bob should receive two votes for Secretary.",
"3. "
);
assert(
(voteCount.treasurer["Bob"] === 4),
"Bob should receive four votes for Treasurer.",
"4. "
);
assert(
(officers.president === "Louise"),
"Louise should be elected President.",
"5. "
);
assert(
(officers.vicePresident === "Hermann"),
"Hermann should be elected Vice President.",
"6. "
);
assert(
(officers.secretary === "Fred"),
"Fred should be elected Secretary.",
"7. "
);
assert(
(officers.treasurer === "Ivy"),
"Ivy should be elected Treasurer.",
"8. "
);