You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FreeCodeCamp测试:这段updateRecords函数代码能否简化?

Hey there! Let's break down your updateRecords function for that FreeCodeCamp challenge. First off, the code you've started is totally on the right track—your core logic for handling different property types is valid and compliant with the challenge requirements. But we can definitely trim it down to be more concise and easier to read, plus let's fix that incomplete final case first.

First, let's fill in the missing part of your code to make it fully functional (I'm guessing you cut off the delete line):

Your Working Original Code

function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== "") {
    collection[id][prop] = value;
  } else if(prop == "tracks" && collection[id].hasOwnProperty(prop) === false){
    collection[id][prop] = [];
    collection[id][prop].push(value);
  } else if(prop == "tracks" && value !== ""){
    collection[id][prop].push(value);
  } else if(value === ""){
    delete collection[id][prop];
  }
  return collection; // Don't skip this—FreeCodeCamp expects you to return the updated collection!
}

This code will absolutely pass the test—all the logic checks out for the challenge's rules. Now let's make it cleaner.

Simplified, More Readable Version

We can cut down on redundant checks and streamline the flow:

function updateRecords(id, prop, value) {
  // Grab the specific record we're modifying to avoid repeating collection[id] everywhere
  const record = collection[id];

  if (value === "") {
    // Empty value? Delete the property, no matter what it is
    delete record[prop];
  } else if (prop === "tracks") {
    // For tracks: if the array doesn't exist, create it first, then add the value
    record[prop] = record[prop] || [];
    record[prop].push(value);
  } else {
    // All other non-empty values just get assigned directly
    record[prop] = value;
  }

  return collection;
}

What Makes This Better

  • Less repetition: Storing collection[id] in a record variable saves us typing the same thing over and over, and makes the code cleaner.
  • Smarter condition order: We handle the universal "empty value" case first—this is a rule that applies to all properties, so it makes sense to get it out of the way.
  • Concise tracks initialization: The line record[prop] = record[prop] || [] replaces the separate hasOwnProperty check. If the tracks property doesn't exist, record[prop] is undefined (falsy), so we create a new array. If it does exist, we just use the existing array.
  • Fewer conditionals: We went from 4 nested if/else if blocks to 3, with logic that's easier to follow at a glance.

Quick Compliance Check

Just make sure you return the collection at the end—this is a super common mistake in this challenge, and your original code was missing that line (I added it in both versions). Both the original and simplified versions meet all the challenge's requirements:

  • Updates non-tracks properties when value is non-empty
  • Creates a tracks array if it doesn't exist when adding a track
  • Adds new tracks to existing arrays
  • Deletes any property when the value is empty

Your initial code was totally viable, but the simplified version is more maintainable and reads better. Either one will pass the test with flying colors!

内容的提问来源于stack exchange,提问作者ajhernandez95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:09:17