Created
February 3, 2014 20:18
-
-
Save brittanmcg/8791527 to your computer and use it in GitHub Desktop.
This script goes through the votes object and it's nested object and counts the number of times that each candidate is voted for and then tallies them in a separate object. Then we need to somehow return a winner for each category.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In this challenge you will work with the following JavaScript objects. | |
Do not alter these objects here. | |
*/ | |
// I need to go through the votes object and look at each string which contains it's own object, then I need to go through | |
// each nested object and count how many times a canidites name comes up for each category in the votes count object and assign it | |
// to that object. We need to look at each property inside the nested objects and count how many times that value occurs. | |
// 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. After Alex's votes have been tallied, | |
voteCount would be ... | |
var voteCount = { | |
president: { Bob: 1 }, | |
vicePresident: { Devin: 1 }, | |
secretary: { Gail: 1 }, | |
treasurer: { Kerry: 1 } | |
} | |
*/ | |
/* 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 count = function(votes) { | |
for (var key in votes){ | |
var voters = votes[key]; | |
for (var position in voters){ | |
var vote = voters[position]; | |
if(position === "president"){ | |
if(voteCount.president.hasOwnProperty(vote)){ | |
voteCount.president[vote] = voteCount.president[vote] + 1;} | |
else {voteCount.president[vote] = 1; } | |
} //if position == president end bracket | |
if(position === "vicePresident"){ | |
if(voteCount.vicePresident.hasOwnProperty(vote)){ | |
voteCount.vicePresident[vote] = voteCount.vicePresident[vote] + 1;} | |
else {voteCount.vicePresident[vote] = 1; } | |
} | |
if(position === "secretary"){ | |
if(voteCount.secretary.hasOwnProperty(vote)){ | |
voteCount.secretary[vote] = voteCount.secretary[vote] + 1;} | |
else {voteCount.secretary[vote] = 1; } | |
} | |
if(position === "treasurer"){ | |
if(voteCount.treasurer.hasOwnProperty(vote)){ | |
voteCount.treasurer[vote] = voteCount.treasurer[vote] + 1;} | |
else {voteCount.treasurer[vote] = 1; } | |
} | |
} // for position in voters end bracket | |
} // for key in votes end bracket | |
return voteCount; }; //function(votes) end bracket | |
count(votes); | |
var winner = function(count){ | |
}; | |
winner(voteCount.president); | |
// __________________________________________ | |
// 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. " | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment