Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
desktop.ini
60 changes: 60 additions & 0 deletions index.js
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
Copy link
Member

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!

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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of Set


// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of max

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
Copy link
Member

Choose a reason for hiding this comment

The 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.