简体   繁体   中英

Debugging a JavaScript Freecodecamp challenge

I have been stumped by this challenge and I really don't understand why my code won't work. The challenge is "Basic JavaScript: Record Collection" from Freecodecamp.

The challenge is:

"If the value parameter isn't an empty string, update (or set) the value parameter for the prop parameter. If the prop parameter is equal to "tracks" and the value isn't an empty string, push the value onto the end of the tracks array. If value is an empty string, delete that prop from the object. Finally, return the collection object.

Code:

 var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {

  if (prop === "tracks" && value !== "") {
    if (collection.id.hasOwnProperty(prop)) {
    collection[id][prop].push(value);
    }
  else {
    collection.id.prop = [value];
  }
}

  else if (value !== "") {
    collection.id.prop = value;
  }

  else {
    delete collection.id.prop;
  }

  return collection;
}

// Alter values below to test your code
updateRecords(2468, "tracks", "ABBA");

Try this, I've added comments for clarity. Let me know if you don't understand something.

 var collection = {
  "2548": {
    "album": "Slippery When Wet",
    "artist": "Bon Jovi",
    "tracks": [ 
      "Let It Rock", 
      "You Give Love a Bad Name" 
    ]
  },
  "2468": {
    "album": "1999",
    "artist": "Prince",
    "tracks": [ 
      "1999", 
      "Little Red Corvette" 
    ]
  },
  "1245": {
    "artist": "Robert Palmer",
    "tracks": [ ]
  },
  "5439": {
    "album": "ABBA Gold"
  }
};

function updateRecords(id, prop, value) {
  // Exit function if required items are missing
  if (!id || !collection) return;

  // We can't assume that collection will have [id]
  if (!collection.hasOwnProperty(id)) {
    collection[id] = [];
  }

  // Create an object reference to the id in the collection
  const reference = collection[id];

  // Now it's pretty straight forward after
  if (prop === "tracks" && value && typeof value === "string") {
    if (reference.hasOwnProperty(prop)) {
      reference[prop].push(value);
    } else {
      reference[prop] = [value];
    }
  } else if (value) {
    reference[prop] = value;
  } else {
    delete reference[prop];
  }

  return collection;
}

// Alter values below to test your code
updateRecords(2468, "tracks", "ABBA");
updateRecords(2468, "tracks", ["BETA"]);
updateRecords(2468, "tracks", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM