0

I'm creating a function and stuck on this prompt:

updateObject() : Should take an object, a key and a value. Should update the property key on object with new value. If does not exist on create it.

This is my code:

function updateObject(object, key, value) {
if (object.key) {
    return object.key = value;
} else if (object.key === false) {
    return object.key;
}

}

This is the test it's trying to pass:

 QUnit.test("updateObject() : Should take an object, a key and a value. Should update the property <key> on <object> with new <value>. If <key> does not exist on <object> create it.", function(assert){
  var data = {a: "one", b: "two", "hokey": false};
  assert.deepEqual(updateObject(data, "b", "three"), {a:"one", b:"three", hokey: false});
  var data = {a: "one", b: "two", "hokey": false};
  assert.deepEqual(updateObject(data, "ponies", "yes"), {a:"one", b:"two", hokey: false, ponies: "yes"});
  var data = {a: "one", b: "two", "hokey": false};
  assert.deepEqual(updateObject(data, "a", Infinity), {a:Infinity, b:"two", hokey: false});
});

What am I doing wrong?

Ashton
  • 63
  • 7
  • 1
    You need to use `object[key]` if `key` is a variable. This is to distinguish it from the actual key named 'key'. – Mark Nov 11 '17 at 03:43
  • Possible duplicate of [How do I add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/how-do-i-add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – Mark Nov 11 '17 at 03:46
  • I thought using bracket and dot notation was interchangeable? Key is actually the property I’m testing against: the first statement testing if there is a key property then to push the value onto the key. If not, create the key property in the second instance of testing to see if there is a key property. – Ashton Nov 11 '17 at 03:54
  • Just consider the `obj = {key: 'k_value', test: 't_value'}` -- If you have a variable `key = 'test'` what should `obj.key` do? – Mark Nov 11 '17 at 04:48
  • Obj.key would return the “test” value in addition to the k_value correct? If key is equal to test, then it would add another value to that property...right? – Ashton Nov 11 '17 at 05:29

2 Answers2

0

Use square bracket if there is a need to access object key using a variable.Also hasOwnProperty can be used to check if object has a key

function updateObject(object, key, value) {
        if (object.hasOwnProperty(key)) {
            return object[key]  // will return value of the key in that object
        } else {
            return object[key]=value; // will create a new key and will assign value
        }
    }
brk
  • 48,835
  • 10
  • 56
  • 78
  • So in the instance where if there is a key property, but no value, how would I amend the first return? – Ashton Nov 11 '17 at 04:09
0

You need to return the updated object from the function for the tests to pass.

function updateObject(object, key, value) {
  object[key] = value;
  return object;
}
Nikhil Fadnis
  • 850
  • 6
  • 8