Record Collection - dilemma

Record Collection - dilemma
0

#1

Tell us what’s happening:
Hello everyone,
I’ve tested the code below in the browser’s console and it passes all tests, however in the emulator here on freeCodeCamp it does not pass:

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

Any ideas why?

Your code so far

// Setup
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 (value === "") { // if the value is "" delete the property
            delete collection[id][prop]
            console.log(collection)
            return collection
        }

        if (!collection[id].hasOwnProperty(prop)) {
            console.log('Property does not exist, adding it ..')
            if (prop === 'tracks') { // if it's tracks that does not exist, create the array with the value..
                collection[id][prop] = [value] 
                console.log(collection)
                return collection
            }
            collection[id][prop] = value
            console.log(collection)
            return collection
        } else {
            if (prop === 'artist') {
               collection[id][prop] = value
               console.log(collection) 
               return collection
            } else if (prop === 'tracks') { // if it's tracks and it exists, put the value first in the arr
                collection[id][prop].unshift(value)
                console.log(collection)
                return collection
            }
        }
      
    return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

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

Link to the challenge:


#2

It looks like you used the wrong method to insert a value in tracks.


#3

Why wrong? Unshift insert an element at the beginning of the array.

Try the code in a browser and you’ll see it is working.


#4

But you’re supposed to insert an element at the end of the array, not the beginning.


#5

Lol, i’m such an idiot. Thank you for opening my eyes :slight_smile: