Record Collection - Why isn't my code working? Please Explain

Record Collection - Why isn't my code working? Please Explain
0

#1

Can someone please explain why my code is not working.
It passes all the test’s except for one - After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

I have gone through my code many times and I can’t see why it shouldn’t work.

Thank you in advance

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 (prop !== "tracks" && value !== "") {
     collection[id][prop] = value;
  } else if (prop === "tracks" && collection[id].hasOwnProperty("tracks" === false)) {
    collection[id][prop] = [].push(value);
  } else if (prop === "tracks" && value !== "") {
    collection[id]["tracks"].push(value);
  } else if (value === "") {
    delete collection[id][prop];
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/


#2

i put your code into repl.it and tried to run it but it had a runtime error.

I believe the problem is this part of your if statement
collection[id].hasOwnProperty(“tracks” === false)

What this does is it asks:
Is “tracks” equal to the boolean value false?

The answer will always be no, that is ‘false’.
Then your code asks if false is a property of the collection… Which is obviously also false.

So just fix that logic.


#3

Thank you

I’ve removed the else if statement and the result is the same. All tests pass except for the same one

After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

the error meassge it gives me is

collection[id].tracks is undefined

My code now looks like this:

// 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 (prop !== "tracks" && value !== "") {
     collection[id][prop] = value;
  } else if (prop === "tracks" && value !== "") {
    collection[id]["tracks"].push(value);
  } else if (value === "") {
    delete collection[id][prop];
  }
  
  return collection;
}

#4

i believe you need to go back and review the instructions. You missed one part of steps that describes what to do when tracks is not defined.