Created
December 29, 2022 13:43
-
-
Save DevGW/a6209a7eb6da9d265f05e31be8df5f42 to your computer and use it in GitHub Desktop.
Javascript :: Recursion II Arrays and Objects
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
// adding all the elements of a limitless nested array | |
function arraySum(anArr) { | |
let sum = 0; | |
for (let i = 0; i < anArr.length; i++) { | |
let element = anArr[i]; | |
if (Array.isArray(element)) { | |
sum += arraySum(element); | |
} else | |
sum += element; | |
} return sum; | |
} | |
// all elements are true or false (the long way): | |
function allSystemsGo(systems) { | |
for (let key in systems) { | |
let value = systems[key]; | |
if (typeof value === 'object') { | |
let subsystemsGo = allSystemsGo(value); | |
if (!subsystemsGo) { | |
return false; | |
} | |
} if (!value) { | |
return false; | |
} | |
} return true; | |
} | |
// doing the same thing but using .every method: | |
function allSystemsGo(systems) { | |
// Grab all the values of the current system | |
const currentSystemValues = Object.values(systems); | |
// Run an 'every' test to confirm 'every' system is go for launch | |
return currentSystemValues.every((boolOrSystem) => { | |
// If its an object, recurse on the object making sure it is all true values or true systems | |
if (typeof boolOrSystem === 'object') return allSystemsGo(boolOrSystem); | |
// Otherwise simply return the value of the boolean - any false will force the 'every' call to fail. | |
else return boolOrSystem; | |
}) | |
} | |
// return the number of truthy values in an array | |
function theTruthCounts(anArr) { | |
// Store the number of truthy vals | |
let truthyVals = 0 | |
// Iterate through the array | |
anArr.forEach((val) => { | |
// check if the current element is an array | |
if (Array.isArray(val)) { | |
// if so, recurse and add the total number of truthy values inside that array to the number here | |
truthyVals += theTruthCounts(val); | |
// otherwise, add the truthiness of the value to the total | |
} else if (val) ++truthyVals; | |
}) | |
// return the number of truthy values | |
return truthyVals; | |
} | |
// we could also use .reduce instead of .forEach | |
function theTruthCounts(anArr) { | |
// Iterate through the array | |
return anArr.reduce((sum, val) => { | |
// check if the current element is an array | |
if (Array.isArray(val)) { | |
// if so, recurse and add the total number of truthy values inside that array to the number here | |
return sum + theTruthCounts(val); | |
// otherwise, add the truthiness of the value to the total | |
} else if (val) return sum + 1; | |
// return the number of truthy values | |
return sum; | |
}, 0) | |
} | |
// find the longest name in nested objects: | |
function getLongestName(familyTree) { | |
let longestName = ''; | |
familyNames = Object.keys(familyTree); | |
familyNames.forEach((name) => { | |
if (name.length >= longestName.length) longestName = name; | |
if (familyTree[name]) { | |
const longestSubName = getLongestName(familyTree[name]); | |
if (longestSubName.length > longestName.length) longestName = longestSubName; | |
} | |
}) | |
return longestName; | |
} | |
// search for a person in the world to get addresses: arrays, strings, and objets | |
function searchParty(name, world) { | |
// grab names of all places in the current hierarchy and store them | |
let places = Object.keys(world); | |
// iterate through the places | |
for (let i = 0; i < places.length; i++) { | |
let place = places[i]; | |
// store the value of whatever is at that location | |
let nextValue = world[place]; | |
// if person is at that location, then | |
if (nextValue === name) { | |
// return that location | |
return [place]; | |
// if the vallue is an array of multiple people | |
} else if (Array.isArray(nextValue)) { | |
// discover if the person we are looking for is one of the people in the array | |
const isInArray = nextValue.some(person => person === name); | |
// if so, then we found the place, so return it | |
if (isInArray) return [place]; | |
// otherwise, if the value of whats at the location are more sub locations then | |
} else if (typeof nextValue === 'object') | |
// recurse on the sublocation and figure out if they are inside of it | |
const inNextLocation = searchParty(name, nextValue); | |
// if the person is inside of it, add on their location to the current location to get the full address | |
if (inNextLocation) return [place].concat(inNextLocation); | |
} | |
} | |
// if we could not find the person, return null | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment