Is this a bug from FCC or is my code?

Is this a bug from FCC or is my code?
0

#1

Tell us what’s happening:

Hello, i have a question, why if i put:
collection[id][prop] instead of collection[id][“tracks”] it dosen’t work?
Is this a bug or what? I really don’t get it why it dosen’t work

If you have better idea how to write this code cleaner (i am new and i want to get good practice)

Thank you world!

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));

[spoiler]// Only change code below this line
function updateRecords(id, prop, value) {
  if(value == ""){
    delete collection[id][prop];
  }
  else if(prop != "tracks" && value != ""){
    collection[id][prop] = value;
  }
  else if(prop = "tracks" && value != "" && !collection[id]["tracks"]){
      collection[id]["tracks"] = [];
      collection[id]["tracks"].push(value);
  }
  else if(prop = "tracks" && value != "" && collection[id]["tracks"]){
      collection[id]["tracks"].push(value);      
  }

  return collection;
}[/spoiler]

// Alter values below to test your code
updateRecords(5439, "tracks", "Take a Chance on Me")

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36 Avast/67.1.664.99.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collectionThis text will be blurred


#2

The error occurs because in your if statements you wrote:

prop = “tracks”

This assigns the value “tracks” to the ‘prop’ variable. Instead of ‘=’, you should use ‘==’ or ‘===’ to check for equality instead. Then you can use the ‘prop’ value.


#3

Make sure to blur your code when posting so as to avoid people seeing a full working solution immediately. You can highlight the code block, click on the gear for Options, then select “Blur Spoiler” to hide the code.

id is a variable passed to the function so is defined for the code. When you use tracks instead of "tracks", you’re specifying a variable that should be used to access the collection, not the key itself. If you’d set a variable named tracks and used it instead, it’d work.

let tracks = "tracks";
collection[id][tracks].push(value)

Also, as the other poster noted, prop = "tracks" instead of prop === "tracks" in the conditional sets the value of prop to “tracks” instead of comparing it to the value.


#4

In my Experience,
99% of the Threads in this Forum referring to a “Bug” are user mistakes.

I’ve finished 723 Challenges and there are < 5 challenges with Bugs. ( < 0,7% Bug Rate)


#5

OMG thank you! i was really confused!


#6

Thank you for the tip!
P.s I blurred the code! :smiley:


#7

I was 99% sure that is my problem but i really didn’t saw it! :smiley: Sorry