2

im studying JavaScript and im trying to solve the problem in this test exercise: FreeCodeCamp Record Collection

I can't understand why it doesnt work. The object details and the problem description are in the link above.

function updateRecords(object, id, prop, value) {
  if (value === '') {
    delete object[id][prop];
  } else if (prop === 'tracks') {
    if (object[id][prop].hasOwnProperty('tracks') == false) {
      object[id][prop] = [value];
    } else if (value !== '') {
      object[id][prop].push(value);
    }
  } else if (prop !== 'tracks' && value !== '') {
    object[id][prop] = value;
  }
  return object;
}

This is the error i get:

// running tests
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"), tracks should have Take a Chance on Me as the last element.
After updateRecords(collection, 2468, "tracks", "Free"), tracks should have 1999 as the first element.
// tests completed

Thank you for your support.

1 Answer 1

1

Let's take a look at this line:

if (object[id][prop].hasOwnProperty('tracks') == false) {

If we replace the variables with their values, we get:

if (object[5439]['tracks'].hasOwnProperty('tracks') == false) {
                    ^                        ^

... which is always going to fail. Here is a simplified version:

function updateRecords(object, id, prop, value) {
  if (value === '') {
    delete object[id][prop];
  } else if (prop === 'tracks') {
    if (!object[id].hasOwnProperty('tracks')) {
      object[id][prop] = [];
    }
    object[id][prop].push(value);
  } else {
    object[id][prop] = value;
  }
  return object;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.