Last active
March 19, 2019 18:25
-
-
Save NickBolles/3531107d799161bdb705a1a6d13802b6 to your computer and use it in GitHub Desktop.
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
/* | |
Ahoy Matey! | |
Welcome to the seven seas. | |
You are the captain of a pirate ship. | |
You are in battle against the royal navy. | |
You have cannons at the ready.... or are they? | |
Your task is to check if the gunners are loaded and ready, if they are: Fire! | |
If they aren't ready: Shiver me timbers! | |
Your gunners for each test case are 4 or less. | |
When you check if they are ready their answers are in a dictionary and will either be: aye or nay | |
Firing with less than all gunners ready is non-optimum (this is not fire at will, this is fire by the captain's orders or walk the plank, dirty sea-dog!) | |
If all answers are 'aye' then Fire! if one or more are 'nay' then Shiver me timbers! | |
*/ | |
// For in Loop | |
const cannonsReadyForIn = (gunners) => { | |
// start off assuming everyone is ready, if someone isn't then we set this to false | |
let allGunnersReady = true; | |
// for in loop to loop over the keys in the gunners object. We will have to get the values out later | |
for (var name in gunners) { | |
// gunner is the gunner name, so we need to get the answer out of the object still | |
let gunnerAnswer = gunners[name]; | |
// now let's check if the gunner's answer is good or not | |
var gunnerIsReady = gunnerAnswer === 'aye'; | |
// if it's not then set the all gunners ready to false | |
if (!gunnerisReady) { | |
allGunnersReady = false; | |
} | |
} | |
return allGunnersReady ? 'Fire!' : 'Shiver me timbers!'; | |
} | |
// For in Loop with short circuit (Matt's idea!) | |
const cannonsReadyForInShortCircuit = (gunners) => { | |
// for in loop to loop over the keys in the gunners object. We will have to get the values out later | |
for (var name in gunners) { | |
// gunner is the gunner name, so we need to get the answer out of the object still | |
let gunnerAnswer = gunners[name]; | |
// now let's check if the gunner's answer is good or not | |
var gunnerIsReady = gunnerAnswer === 'aye'; | |
// if it's not then set the all gunners ready to false | |
if (!gunnerisReady) { | |
return 'Shiver me timbers!'; | |
} | |
} | |
return 'Fire!'; | |
} | |
// For-Of Loop | |
const cannonsReadyForOf = (gunners) => { | |
// start off assuming everyone is ready, if someone isn't then we set this to false | |
let allGunnersReady = true; | |
// for of loop to loop over the values in gunner (we don't care about the names) | |
for (var gunnerAnswer of gunners) { | |
var gunnerIsReady = gunnerAnswer === 'aye'; // answer is the value here | |
if (!gunnerisReady) { | |
allGunnersReady = false; | |
} | |
} | |
return allGunnersReady ? 'Fire!' : 'Shiver me timbers!'; | |
} | |
// Object.keys with array methods | |
const cannonsReadyObjKeys = (gunners) => { | |
let allGunnersReady = Object.keys(gunners).every(gunnerName => gunners[gunnerName] === 'aye') | |
return allGunnersReady ? 'Fire!' : 'Shiver me timbers!'; | |
} | |
// Object.values with array methods | |
const cannonsReadyObjVals = (gunners) => { | |
let allGunnersReady = Object.values(gunners).every(gunnerAnswer => gunnerAnswer === 'aye') | |
return allGunnersReady ? 'Fire!' : 'Shiver me timbers!'; | |
} | |
// Object.keys with array methods 1 liner | |
const cannonsReadyObjKeysOneLine = (gunners) => Object.keys(gunners).every(gunnerAnswer => gunnerAnswer === 'aye') ? 'Fire!' : 'Shiver me timbers!'; | |
/************************************************************ | |
/* MY SOLUTION | |
/************************************************************/ | |
// Object.values with array methods 1 liner | |
const cannonsReady = (gunners) => Object.values(gunners).every(gunnerAnswer => gunnerAnswer === 'aye') ? 'Fire!' : 'Shiver me timbers!'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment