Basic JavaScript - Record Collection I saw the given solution ,but I don't understand one test case

The test case I don’t understand is that:

updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me");

for “5439”, I expected to get

 '5439': { albumTitle: 'ABBA Gold', tracks: [ 'Take a Chance on Me' ] }

and I got it.

When I tried solution 2, for ‘5439’, I got

'5439': 
   { albumTitle: 'ABBA Gold',
     tracks: [ 'Take a Chance on Me', 'Take a Chance on Me' ] }. 

I’m confused, why this is expected ?

// 设置
const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// 只修改这一行下面的代码
function updateRecords(records, id, prop, value) {
  let record = records[id];
  if (prop !== 'tracks' && value !== '') {
    record = {
      ...record,
      [prop]: value,
    }
  } else if (prop === 'tracks' && !Object.keys(record).includes('tracks')) {
    let tracks = [];
    tracks.push(value)
    record = {
      ...record,
      tracks,
    }
    // console.log('record', record);
  } else if (prop === 'tracks' && value !== '') {
    record = {
      ...record,
      tracks: [...record.tracks, value],
    };
  } else if (value === '') {
    delete record[prop];
  }

  records = { ...records, [id]: record };
  return records;
}

updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me");

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

What solution 2?

That is not the expected solution


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.