Alexander Praetorius
@serapath
it's just .... a person can have an array (=list) of children
Desiree Galeski
@dgales4130
ok
Alexander Praetorius
@serapath
i'm reading it as Beth Johnson from San Francisco has 2 childrenJoshi with no children and Beth Jr. who is parent of Smitty
you can simplify your function to something like
function filterFamilyMembers (familyTree, truthTest) {
  var trueMembers = []
  var familyMember = familyTree
  var rootBranch = [familyMember]
  searchTree(rootBranch)
  return trueMembers
  function searchTree (branch) {
    // @TODO: apply test and add true members to result
    if (!branch.children) return // base case
    if (Array.isArray(branch.children)) return searchTree(branch.children) // recursive case: if (Array.isArray(branch.children))
  }
}
which doesnt work yet... so we need to think about it more
Desiree Galeski
@dgales4130
but would familyTree keep reasigning branch to the top ?
Alexander Praetorius
@serapath
hm i see now why you had the nested thing. you are right, its needed
i adapted the example
can you read and understand it?
Desiree Galeski
@dgales4130
hold on
I am trying to learn more on my own. If you could ask me more questions than tell me code , like that of the base case and the recursive case then I think I'd benefit from that-- if that make since.
I hope that comes off as kind and reasonable.
Alexander Praetorius
@serapath
i'm not sure what to ask
do you understand the re-written example?
Desiree Galeski
@dgales4130
yes indeed. I can ask more questions too.
Alexander Praetorius
@serapath
let me copy & paste your code again but a little bit re-written
Desiree Galeski
@dgales4130
this is what i tried


var filterFamilyMembers = function (familyTree, truthTest) {


      // base case : if familyTree.Children  ===  false 
            // array 


  var result = []; 

      function searchTree(branch, truthTest) {


       if(truthTest(branch)) {
          result.push(branch);
        }

        if (Array.isArray(branch.children)){
          for(person in branch){
          searchTree(person,truthTest)  
          }

        }

     searchTree(familyTree); 
     return result; 
};
Alexander Praetorius
@serapath
/*******************************************************************************
  DATA
*******************************************************************************/
var familyTree = {
  firstName: 'Beth',
  lastName: 'Johnson',
  location: 'San Francisco',
  children: [{
    firstName: 'Beth Jr.',
    lastName: 'Johnson',
    location: 'Berkeley',
    children: [{
      firstName: 'Smitty',
      lastName: 'Won',
      location: 'Beijing',
      children: []
    }]
  },{
    firstName: 'Joshie',
    lastName: 'Wyattson',
    location: 'Berkeley',
    children: []
  }]
}
/*******************************************************************************
  USE
*******************************************************************************/
var result = filterFamilyMembers(familyTree,livesInBerkeley)
console.log(result) // ['Beth Jr. Johnson', 'Joshie Wyattson']

/*******************************************************************************
  filterFamilyMembers 
*******************************************************************************/
function filterFamilyMembers (familyTree, truthTest) {


      // base case : if familyTree.Children  ===  false 
            // array 


  var result = []; 

      function searchTree(branch, truthTest) {

        for( var person in branch){
          console.log(person.firstName);
        }
        if(branch.children.length > 0){
          searchTree(branch.children,truthTest); 
        }
         return result;
      }
     debugger;
     searchTree(familyTree); 
}
/*******************************************************************************
  HELPER 
*******************************************************************************/
function livesInBerkeley (familyMember) { return familyMember.location === 'Berkeley' }
do you find all your code in this re-written example?
then we could start from here
Desiree Galeski
@dgales4130
I think my last code snippet is much closer , would you agree ?
I am getting a Uncaught SyntaxError: Unexpected end of input error in the console
oops scope issues on my function call ... this one I fixed that :
var filterFamilyMembers = function (familyTree, truthTest) {


      // base case : if familyTree.Children  ===  false 
            // array 


  var result = []; 

      function searchTree(branch, truthTest) {

       if(truthTest(branch)) {
          result.push(branch);
        }

        if (Array.isArray(branch.children)){
          //for(person in branch){
            searchTree(branch.children,truthTest); 
        // }
        }

   }
  searchTree(familyTree); 
  return result;
};
Alexander Praetorius
@serapath
looks good
does it work?
Desiree Galeski
@dgales4130
nop
nope it says truthTest is not a function
Alexander Praetorius
@serapath
i think the problem is, that branch is an array
oh i see
probably it's because the way you write your code. when you call filterFamilyMembers(...) the function you pass in as an argument, (the truth tests), is not yet defined
did you already learn about array methods?
.filter, .map, .reduce, ...?
truthTest is applied to a single member
so you can't test an array of members, but need to test each array element (=member) individually
Desiree Galeski
@dgales4130
yep
Alexander Praetorius
@serapath
/*******************************************************************************
  DATA
*******************************************************************************/
var familyTree = {
  firstName: 'Beth',
  lastName: 'Johnson',
  location: 'San Francisco',
  children: [{
    firstName: 'Beth Jr.',
    lastName: 'Johnson',
    location: 'Berkeley',
    children: [{
      firstName: 'Smitty',
      lastName: 'Won',
      location: 'Beijing',
      children: []
    }]
  },{
    firstName: 'Joshie',
    lastName: 'Wyattson',
    location: 'Berkeley',
    children: []
  }]
}
/*******************************************************************************
  USE
*******************************************************************************/
var result = filterFamilyMembers(familyTree,livesInBerkeley)
console.log(result) // ['Beth Jr. Johnson', 'Joshie Wyattson']

/*******************************************************************************
  filterFamilyMembers 
*******************************************************************************/
function filterFamilyMembers (familyTree, truthTest) {
  var result = []
  // each `familyTree` is also a `familyMember`,
  // but a `branch` is an array of `(sub)familyMembers`, thus an Array of `(sub)familyTrees`
  searchTree([familyTree]) // `searchTree(...)` expects an array of familyMembers
  return result
  function searchTree (branch) {
    branch.forEach(function test (member) {
      if (truthTest(member)) result.push(member)
      if (Array.isArray(branch.children)) searchTree(branch.children) // RECURSIVE CASE
      // else do nothing (BASE CASE)
    })
  }
}
/*******************************************************************************
  HELPER 
*******************************************************************************/
function livesInBerkeley (familyMember) { return familyMember.location === 'Berkeley' }
Alexander Praetorius
@serapath
The above example has 2 little mistakes which would fix the whole thing
It's inside the searchTree function
if fixed, it gives the correct result.
  • One is about the recursion
  • The other one is about extracting the correct data from a trueMember, so that we gather the names
Alexander Praetorius
@serapath
If you read the above code line by line? can you follow?
If something is unclear I can try to explain. If you get stuck, I can also say what the "2 little mistakes" are
Desiree Galeski
@dgales4130
ohhhhh
Let me look at it for a bit
Desiree Galeski
@dgales4130
I don't think I am allowed to change the type of family tree :(
Alexander Praetorius
@serapath
what do you mean with "type"?
Alexander Praetorius
@serapath
no type is changed
Desiree Galeski
@dgales4130
hmmm
I need a break. Thanks for the help.
Alexander Praetorius
@serapath
ok. here is a quick version of the fixed code that works @dgales4130
you can compare it with the version above to see what changed.
/*******************************************************************************
  DATA
*******************************************************************************/
var familyTree = {
  firstName: 'Beth',
  lastName: 'Johnson',
  location: 'San Francisco',
  children: [{
    firstName: 'Beth Jr.',
    lastName: 'Johnson',
    location: 'Berkeley',
    children: [{
      firstName: 'Smitty',
      lastName: 'Won',
      location: 'Beijing',
      children: []
    }]
  },{
    firstName: 'Joshie',
    lastName: 'Wyattson',
    location: 'Berkeley',
    children: []
  }]
}
/*******************************************************************************
  USE
*******************************************************************************/
var result = filterFamilyMembers(familyTree,livesInBerkeley)
console.log(result) // ['Beth Jr. Johnson', 'Joshie Wyattson']

/*******************************************************************************
  filterFamilyMembers 
*******************************************************************************/
function filterFamilyMembers (familyTree, truthTest) {
  var result = []
  // each `familyTree` is also a `familyMember`,
  // but a `branch` is an array of `(sub)familyMembers`, thus an Array of `(sub)familyTrees`
  searchTree([familyTree]) // `searchTree(...)` expects an array of familyMembers
  return result
  function searchTree (branch) {
    branch.forEach(function test (member) {
      if (truthTest(member)) result.push(member.firstName + ' ' + member.lastName)
      if (Array.isArray(member.children)) searchTree(member.children) // RECURSIVE CASE
      // else do nothing (BASE CASE)
    })
  }
}
/*******************************************************************************
  HELPER 
*******************************************************************************/
function livesInBerkeley (familyMember) { return familyMember.location === 'Berkeley' }
erjavecp
@erjavecp
hi
Alexander Praetorius
@serapath
hi :-)