简体   繁体   English

调试JavaScript Freecodecamp挑战

[英]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. 挑战是Freecodecamp的“基本JavaScript:记录收集”。

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. “如果value参数不是空字符串,请更新(或设置)prop参数的value参数。如果prop参数等于“ tracks”并且该值不是空字符串,则将值压入如果数组中的值是一个空字符串,则将其从对象中删除,最后返回集合对象。

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", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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