Problem 23

Count value in object Recursion Challenge

Given a nested object and a target value, return the number of times that value appears anywhere in the object, including inside nested objects. Only count leaf values, not nested object values.

Function Signature

countValuesInObj(obj, value)

Parameters

  • obj — an object that may contain nested objects.
  • value — the target value to count.

Output

Return a number representing how many times the value appears in the object tree.

Constraints

  • The function must use recursion.
  • The function should accept exactly two arguments.

Examples

// Given: obj = {e: {x: 'y'}, t: {r: {e: 'r'}, p: {y: 'r'}}, y: 'e'}
countValuesInObj(obj, 'r') → 2
countValuesInObj(obj, 'e') → 1
countValuesInObj(obj, 'y') → 1
countValuesInObj(obj, 't') → 0