-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Answer #1
base: master
Are you sure you want to change the base?
Answer #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
desktop.ini |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
function validateLineup(inputLineup){ | ||
// if the lineup is too long, no need to do any calculations | ||
if (inputLineup.length > 9) return false | ||
|
||
|
||
// calculate our total salary, if it's more than %45,000 return false and exit function | ||
const totalSalary = inputLineup.reduce((previous, array) => previous + array.salary, 0) | ||
if (totalSalary >= 45000) return false | ||
|
||
// Make sure too many teammates aren't on the same team, if so, return false | ||
if(countUniqueProperties(inputLineup, 'teamId', 2)) return false | ||
// Same function can be used for same game away well | ||
if (countUniqueProperties(inputLineup, 'gameId', 3)) return false | ||
|
||
// Now check our positions, this is in a new function due to how big it is | ||
if(!invalidPositions(inputLineup)) return false | ||
|
||
// if nothing has been returned yet..then return true as all conditions must have been true | ||
return true | ||
|
||
|
||
} | ||
|
||
function countUniqueProperties(array, propertyName, numberAllowed){ | ||
// make an array to hold our results | ||
let resultArray = [] | ||
|
||
// make an array of every result we are looking for | ||
const allResults = array.map(array => array[propertyName]) | ||
// now trim all results and get each unique value | ||
const uniqueResults = [...new Set(allResults)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of |
||
|
||
// now count each unique results entries, and add it as an object in our array | ||
uniqueResults.forEach(element => { | ||
resultArray.push(allResults.filter(name => name === element).length ) | ||
|
||
}); | ||
|
||
// now check our array to see if there are more then allowed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of |
||
return Math.max(...resultArray) > numberAllowed | ||
} | ||
|
||
function invalidPositions(array){ | ||
// make an array of every result we are looking for | ||
let players = array.map(array => array.position) | ||
|
||
// now check if there is 3 OF, and remove from array | ||
let outFieldArray = players.filter(player => player === 'OF') | ||
let otherArray = players.filter(player => player != 'OF') | ||
|
||
// now we check if our OF count is correct and our players are all unique positions and there are 6 of them | ||
// if all 3 is true then we have 3 OF and a P, C, 1B, 2B, 3B, and SS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever solution! |
||
return JSON.stringify(otherArray) === JSON.stringify([...new Set(otherArray)]) && outFieldArray.length === 3 && | ||
otherArray.length === 6 | ||
|
||
|
||
} | ||
|
||
|
||
module.exports = validateLineup |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple and accurate. Nice work!