6 kyu

Tree Depth

462 of 526leesharma

Description:

Write a method that takes a nested hash (object in javascript) as input and returns that hash with added "depth" keys. So, for example, the following input:

tree = { 
  a: 1,
  b: 2, 
  c: { d: { e: 3 } }
}
{ a: 1, b: 2, c: { d: { e: 3 } } }

would yield the following return value:

{
  a: 1,
  b: 2,
  c: {
    d: {
      e: 3,
      depth: 2
    },
    depth: 1
  },
  depth: 0
}
{ a: 1, b: 2, c: { d: { e: 3, depth: 2 }, depth: 1 }, depth: 0 }

For Ruby, if the input is not a hash, then the function should return nil. For JavaScript, if the input is not an object (including an array), the function should return null.

Trees
Recursion
Fundamentals

Similar Kata:

Stats:

CreatedAug 15, 2015
PublishedAug 15, 2015
Warriors Trained1063
Total Skips47
Total Code Submissions4047
Total Times Completed526
Ruby Completions71
JavaScript Completions462
Total Stars16
% of votes with a positive feedback rating85% of 110
Total "Very Satisfied" Votes85
Total "Somewhat Satisfied" Votes17
Total "Not Satisfied" Votes8
Total Rank Assessments14
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • leesharma Avatar
  • Abbe Avatar
  • Voile Avatar
  • FArekkusu Avatar
  • cliffstamp Avatar
Ad