Basic JavaScript: Record Collection my solution not work

Tell us what’s happening:
Why not work my solution? I tested local (Vs code). Only first and last test gave pipe symbol. Thank you

Your code so far


// Setup
var collection = {
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'
}
};

// Only change code below this line

function updateRecords(object, id, prop, value) {
  var object2;
  object2 = object
  if(prop == 'albumTitle'){
     object2[id].albumTitle = value;
   return object2;
  }else if(prop == 'artist') {
     object2[id].artist = value;
     return object2;
  }else {        
      if(tracks=""){
       delete collection[id].tracks;
      }
    object2[id].tracks = value;
    return object2;
  }
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

This isn’t exactly the requested logic.

I have a few questions

  1. Why the object2

  2. Why the specific cases for ‘albumTitle’ and ‘artist’ when those are not mentioned as special cases in the instructions

  3. Why is deletion only happening when do not have a ‘albumTitle’ or ‘artist’

  4. Why is that deletion immediately undone?

2 Likes

Thank you.

You get me the right questions that I see the right aspect of the problem. Now I can fix it.

I copy the object because I don’t understand why this sentence:

" Note: A copy of the collection object is used for the tests."
I yet don’t understand…

here is my code that accept as solution the “engine”:

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

Nice work!

To clarify - " Note: A copy of the collection object is used for the tests." just means that you can modify collection during your own experimenting to get the code to work because the test suite will make a brand new copy before the tests are run.

I agree, that note can be confusing.