-3

Alright then implement the objContains function: must search inside a nested object for a pair {key: value} specific. Both the object and the property name and its value will be received by parameter. In the event that it finds the indicated value at any level of the object, it must return true, otherwise return false.

Ex:
const user = {
    id: 6,
    email: 'homero@maxpower.com',
    Personal info: {
        name: 'Homer Simpson',
        address: {
            street: 'Avenue AlwaysLive',
            number: 742,
            neighborhood: 'Springfield',
            state: 'Massachusetts'
        }
    }
}

Case that returns true -> objContains (user, "neighborhood", "Springfield");

Case that returns false -> objContains (user, "employment", "Employee in nuclear plant");

Hint: use typeof to determine if the value of a property is an object to apply there the recursion

this is what i've try

var objContains = function (obj, prop, value) {
   object var = prop.value
  if (typeof prop.value === obj) {
    var current = this.value;
    objContains (object, prop, current)
  } else if (this.prop === prop && prop.value === value) {
      return true;
  } else {
    return false;
  }
}
AssertionError: expected false to equal true

      32 | }
      33 | it ('It should return true if it finds the property and its correct value', fun
ction () {
    > 34 | expect (objContains (user, "neighborhood", "Springfield")). to.equal (true);
      35 | });
      36 | it ('Should return false if the property is NOT found', function () {
      37 | expect (objContains (user, "employment", "Employee at nuclear power plant")). to.equal (
false);
user229044
  • 232,980
  • 40
  • 330
  • 338
Cyndae
  • 7
  • 2
  • What is the `object` part of `object var = prop.value` meant to mean (in the code)? – T.J. Crowder Jul 17 '20 at 15:01
  • **All:** This looks like an assignment that we should *help* the OP with, **without** actually **doing it** for them. – T.J. Crowder Jul 17 '20 at 15:04
  • its for helping me or maybe not idk what am reading right now xd too much hours trying it – Cyndae Jul 17 '20 at 15:05
  • Actually, looking at it again, `object var = prop.value` is a syntax error. That line isn't really doing anything for you, so you can just remove it. (Also, the solution to this assignment won't involve `this`.) – T.J. Crowder Jul 17 '20 at 15:09

2 Answers2

1

There are a couple of problems:

  1. typeof prop.value checks the type of the property with the literal name value on whatever prop is. You've described prop as the name of an object property, so it doesn't make sense to try to look for value on it. You want to look on obj, not prop, using the syntax described in this question's answers. I don't think it's writing it for you to say that that looks like this: if (obj[prop] === value)

  2. If you don't find the property with that value on obj, you should be looking through the other properties on obj and, if any of them refer to objects, calling your function with the object they refer to (and prop and value) and, if the function returns true, return true. Looping through the properties on the object is covered by this question's answers.

So the general form of a solution would be:

  • See if obj has a property whose name is the value of prop that matches the value of value. If so, return true.
  • If not, loop through obj's other properties and check the type of their values; for each one that's an object (typeof is "object" and the value isn't null), call your function again with that property value, prop, and value. If it returns true, return true. If not, keep looping.

I'm purposefully just giving an indication of where to go, rather than writing the code, because this seems like an assignment we should help you with rather than doing for you.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

I loop through each key in object... if the the key refers to an object itself then recursively call... else check to see if that's the key we're looking for and if so check its value... using the |= operator makes it easy to not worry about results of individual calls... we just care if the conditions are true ANYWHERE in the tree..

const user = {
  id: 6,
  email: 'homero@maxpower.com',
  "Personal info": {
    name: 'Homer Simpson',
    address: {
      street: 'Avenue AlwaysLive',
      number: 742,
      neighborhood: 'Springfield',
      state: 'Massachusetts'
    }
  }
}

var objContains = function(obj, prop, value) {
  var returnValue = false;
  var keyArray = Object.keys(obj);
  keyArray.forEach(property => {
    if (typeof(obj[property]) == 'object') {
      returnValue |= objContains(obj[property], prop, value);
    } else if (property == prop) {
      returnValue |= obj[property] == value;
    }
  });

  return !!returnValue;
}

console.log(objContains(user, 'neighborhood', 'Springfield'));
DynasticSponge
  • 1,416
  • 2
  • 9
  • 13