Created
October 8, 2016 07:49
-
-
Save johntran/035c195fdf3061b10ad442eb3dadb591 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function log(input, str) { | |
if (str) return console.log(str, input) | |
console.log('logging', input) | |
return 'Program finished' | |
} | |
// var x = 1; | |
// log(x); | |
// var y = 2; | |
// log(y, 'hihihihihi'); | |
// var arr = [x, y, 3]; | |
// log(arr); | |
function addAllNumber(numberArray) { | |
var result = 0; | |
for (var i = 0; i < numberArray.length; i++) { | |
result = result + numberArray[i]; | |
} | |
return result; | |
} | |
function valueExistsInArray(result, value) { | |
var valueExists = false; | |
for (var y = 0; y < result.length; y++) { | |
if (result[y] === value) { | |
valueExists = true; | |
} | |
} | |
return valueExists; | |
} | |
function noRepeatingValues(valueArray) { | |
var result = []; | |
for (var i = 0; i < valueArray.length; i++) { | |
var value = valueArray[i] | |
var valueExists = valueExistsInArray(result, value) | |
if (valueExists === false) { | |
result.push(value) | |
} | |
} | |
return result; | |
} | |
noRepeatingValues([1, 1, 2]); | |
function getBallotResults(ballot) { | |
var ballotDictionary = {}; | |
for (var i = 0; i < ballot.length; i++) { | |
var name = ballot[i]; | |
var votesForName = ballotDictionary[name] | |
if (votesForName) { | |
ballotDictionary[name] = ballotDictionary[name] + 1; | |
} else { | |
ballotDictionary[name] = 1; | |
} | |
} | |
return ballotDictionary; | |
} | |
function getHighestVotesFromBallot(ballotDictionary) { | |
var result; | |
var ballotDictionaryKeys = Object.keys(ballotDictionary); | |
for (var y = 0; y < ballotDictionaryKeys.length; y++) { | |
var currentName = ballotDictionaryKeys[y]; | |
if (!result) { | |
result = currentName; | |
} else { | |
if (ballotDictionary[result] < ballotDictionary[currentName]) { | |
result = currentName; | |
} | |
} | |
} | |
return result; | |
} | |
function countBallot(ballot) { | |
var ballotDictionary = getBallotResults(ballot); | |
var result = getHighestVotesFromBallot(ballotDictionary); | |
return result; | |
} | |
// countBallot(['John', 'John', 'Mary', 'Mary']) | |
// addAllNumber([1,2,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment