Basic JavaScript: Record CollectionPassed / stuck

Hi there.

I am trying to solve the challenge Basic JavaScript: Record Collection but cannot find a solution. I would like to solve the challenge without peeking into solutions.

I would appreciate it if someone could tell me what am I doing wrong.

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

This is my 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"
  }
};

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

// Alter values below to test your code
console.log(updateRecords(1245, "artist", "ABBA"));

these are just a couple:
you never check the value of parameters prop and value
your return statements are varied, but you are asked to always return the collection object after the changes

I have now removed the parameters prop and value from the first if statement and
I have removed the return and left it only for the end to return the entire collection but immediately after the first if statement I am getting undefined. Why undefined. I mean if the challenge says
If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.

Shouldn’t I include collection[id][prop] to see if the the "tracks" is part of the prop

Below is now changed if statement which returns undefined

if(collection[id] !== collection[id].tracks && collection[id] !== [""]){
		collection[id][prop] = collection[id][prop][value];

I have altered my code like following but still receive a message undefined

function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== [""]){
    collection[id] = value;
  } else if (prop === "tracks" || "tracks" === false ) {
    collection[id] = value;
  } else if (prop === "tracks" && value !== "") {
    collection[id]["tracks"] = push.value;
  } else if (value === "") {
    delete collection.prop
  } else {
    return collection;
  }

}

you are overwriting the whole sub-object, not just a variable
also remember that in an if/else if/else only one of the statements execute, so your return statement will almost never execute - maybe you want to put it after the whole chain?

Thank you very much @ilenia you have helped me a lot with this challenge and with the understanding of objects. I have made a new solution and I am not getting undefined anymore but I am still not passing all the requirements of the challenge.
I have placed my return outside of the statement but I do not understand the part when you say “you are overwriting the whole sub-object, not just a variable”.
My current code is below

function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== [""]){
    collection[id][prop] = value;
  } else if (prop === "tracks" || "tracks" === false) {
    collection[id] = value;
  } else if (prop === "tracks" && value !== "") {
    collection[id][prop] = tracks.push(value);
  } else if (value === "") {
    delete collection.prop
  }

  return collection;
}

string and arrays can’t be compared like that

when prop is tracks one of the objects inside the collection object is overwritten. For updateRecord(1245, "tracks", "song title") it becomes like this

{
   1245: "song title" // no more object
   ...
}

also, "tracks" === false will always give the same value as you are comparing a string and a boolean. Will always evaluate to false. Can be removed.

(there is other stuff below this, I have just mentioned the first couple of things)

1 Like