Need some help with recursive js and json

hi, i am new to javascript and im trying to translate my teachers notes. so if there is “children” then they need to answer something and if there is no children then it goes back to a question asked before and if there are no questions anymore the game is over, how do i translate this?

  const json = '{ 
   "cmd":"SCREEN_TREE",
   "data":{ 
      "id":0,
      "name":"Root",  
      "answers":[],  
      "state":2,
      
      "children":[ 
         { 
            "id":1,
            "name":"People should be shamed for flying",  
            "answers":[      
               { 
                  "user_id":1,    
                  "val":1     //agree
               },
               { 
                  "user_id":2,
                  "val":0     //disagree
               }
            ],               
            "state":4,
            "children":[ 
               { 
                  "id":2,
                  "name":"FOo", 
                  "answers":[],  
                  "state":4
               },
               { 
                  "id":3,
                  "name":"Bar", 
                  "answers":[],  
                  "state":4
               }
            ]               
         }                  
      ]                     
   }
}
'

    document.getElementById("consensus").innerHTML = data.answers.val[1] 
    const consensus = data['data']['children'][0]['children'][0]['name'] 

function f(statement){
  if(child){
    for each(child){
      f(child)
    }else{if(ans=0){    incoming 
    }else if(ans < #incoming) {
      being considered
    }else {cons !bla ignore
    }

    }

  }
}

using if else ? maybe it works if u want a condition to be true