Basic JavaScript: Record Collection _

Basic JavaScript: Record Collection _
0

#1

Guys i am thinking of this code challenge from yesterday and i didn’t come up with any ideas how this works i tried to get some help from other peoples codes who are struggling with the same challenge but still didn’t work.
can anyone explain it in a little better way thanFCC and tell my what wrong with my code.

Your code so far



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



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

#2

Let’s first get rid of backticks at the beginning of your code and the end of your code.

Also you need to spell the name of your function correctly.


#3

My suggestion would be to try your best to follow the instructions linearly, as otherwise some cases are not caught properly.

This means that the first few lines of code should relate to the instruction “If prop isn’t “tracks” and value isn’t empty (”"), update or set the value for that record album’s property.", which would look like:

  if (prop != "tracks" && value != ""){
    collection[strId][prop] = value;
  }

Then onto the next instruction “If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.”, which would look like:

  if (prop === "tracks" && collection[strId]["tracks"] === undefined) {
    collection[strId]["tracks"] = []
  }

(Note: I didn’t push the value onto the array in the above because it is covered in the next instruction.)

And so on… You should not need to use else if statements or elses (of which you have two in a row in your code, causing it to not run). It should just be several if statements and a return collection; at the end.

Good luck and let me know if you need any further clarification.