Record Collection Comparison Operators

Tell us what’s happening:

I’m having issue with the Record Collection, but my main concern right now is what the issue is with the operators.

The first line of if/else necessitates that I use the assignation operator or I don’t pass one of the tests.

As far as the lines of code below this, I am not sure if it makes a difference if I use the the double or triple =. Neither of them pass and I remain stumped for right now, but I have an inkling that I should use exact equality. Could someone confirm or correct this assumption?

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" && collection[id] != value){
  [value] = [];
  collection[id][prop] = value;
}
else if(prop === "tracks" && value !== ""){
  collection[id][prop].push(value);
} 
else if(prop === "tracks" && 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

I am stuck on two last aspects of this challenge.
The first has to do with how I am using .push()
It seems from that I am not defining value, but I am not sure if this assumption.
The second has to do with setting a value for “artist”. I have not attempted to do so so I’m confused as to how I’ve done this and would like a little guidance as to how I’ve managed to do 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(value === "") {
  delete collection[id][prop];
} else if(prop === "tracks" && value !== "") {
  collection[id]["tracks"].push(value);
} else if(prop === ""){
  collection[id][prop] === value;
} else if(collection[id].hasOwnProperty(prop)) {
  collection[id][prop].push(value);
}
  return collection;
}

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

Part of my problem is that I can’t figure out which line of code corresponds to which record. I think I am writing for one line, which seems to satisfy what another line needs.

This problem is currently focused on #5439. The first line was what I wrote in an attempt to solve #5439, but it does not address this problem and I am having difficult figuring out where to proceed from here because this is the only code I can conceive as solving this particular issue.


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

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

Alright, a lot of soul searching and vision questing and all and I’m still at the same impasse. Therefore, I ask for a bit guidance or correction of my logic.

Basically, it is my final line of code below. I have used all suggestions and directions and I am perplexed by how I am left with two records which only need new “tracks” to be pushed to end. What I have done is to define the id and prop as the value and define the value as “tracks”. Then, to use that definition to push “tracks” onto the value and, thereby, nudge to the end of the elements.

I don’t think this is logic is sound; yet, I’m not exactly sure what I’m missing but I am indeed missing something.

Queries are:

  1. Should I be using the === in this case or should I simply be assigning value to collection[id[prop] ?
  2. Should I be writing value and “tracks” in a manner that acknowledges the array type of “tracks”? (E.g. value.tracks ??)

Much obliged for any feedback.

Later.


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

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

I can’t seem to .push “tracks” . “tracks” is my prop and I have created an empty array for value and pushed that value onto the end of the value of “tracks”. I’m not sure where I’m going wrong.

Is it it a syntax thing? Have I done something wrong there?

OR, am I neglecting an element in the code?

OR, is something somewhere else in the code preventing my .push method from carrying through?


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

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

It’s defining for a condition where prop is “tracks” and value is not empty. I want to push something onto the end of “tracks” and I need an array to push that onto.

I’ve been messing around with the code. What I supposed was wrong was that value didn’t need to be a new array because value already had something in it (e.g. value !== “”). So, I wondered if prop needed an array since it equaled “tracks” but did not have the available space to input something into it. This seems flawed to, but I’m stuck as to where I’ve got myself caught. Below is what I’ve come up with so far.

else if(prop === “tracks” && value !== “”) {
prop === [];
collection[id][prop].push(value);
}

I don’t know what I’m doing wrong here.
I think I am creating an empty array so that I can push “tracks” to the end. I’ve re-checked it several times and looked around at other Help posts and, as far as I can tell, I’m doing the same thing that others are doing but their code is working and mine is not. The proper approach is flying right over my head. I know the key is to do with defining “tracks”; yet, I’ve tried this by

prop === "tracks"

as well as

collection[id][prop].push("tracks");

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

I’m really perplexed here. Is there something earlier in my code that’s stopping it from working correctly?



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

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

Okay. I figured it out, but I’d just like an explanation of exactly what I did because I was just messing around for a bit because my brain was starting to go numb.

This as where I seemed to answer correctly but I’m not exactly sure if what I did was proper coding or simply a loophole I found to get a thumbs-up from the system.

  else if(!collection[id].hasOwnProperty("tracks")) {
    collection[id]["tracks"] = [];
    collection[id]["tracks"].push(value);
  }

  else if(value !== "") {
    collection[id][prop] === [];
    collection[id][prop].push(value);

Previously, both of these if statement had the collection[id] with triple equal signs to the empty array. However, I changed the top one to an assignment symbol and the code worked. Does this have something to do with the .hasOwnProperty() method I used or is it something else.

By the way, thanks for your patience with me.


// 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(!collection[id].hasOwnProperty("tracks")) {
    collection[id]["tracks"] = [];
    collection[id]["tracks"].push(value);
  }

  else if(value !== "") {
    collection[id][prop] === [];
    collection[id][prop].push(value);
  }

  else if(value === "") {
    delete collection[id][prop];
  }
 
  return collection;
}

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