Record Collection Help pls

First let me start by saying that I am struggling with JavaScript (obviously). But I won’t let that stop me. Can anyone tell me if my code looks anything like I’m on the right track here? What I have is an else/if with another else/if nested inside of it. The parent e/i is to handle the prop !-- “tracks”. The child e/i is to handle the cases of prop === “tracks”. So this is my first ‘division’ I suppose you’d call it. I can look at my code and know that it obviously isn’t correct, but when I submit the assignment it doesn’t give me ANY of the criteria which leads me to believe that something is wrong with my parent e/i. So any help in getting me in the right direction would be appreciated.

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][prop]hasOwnProperty("tracks"))
    collection[id][prop].push(tracks);
  } else if (prop === "tracks" && value !== "") {
    collection[id][prop][tracks].push(value);
  } else {
    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/66.0.3359.170 Safari/537.36 OPR/53.0.2907.99.

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

See the text marked strong below (I’m still learning the ropes of posting on this forum so bear with me if that’s unclear).

Basically it looks like you’re missing the curly braces on your if statement and you’re also trying to push tracks to a currently non-existent property. To create the property as an empty array you would need to say collection[id].tracks = [] within your if statement.

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

    if (prop === "tracks" && !collection[id][prop]hasOwnProperty("tracks"))
    collection[id][prop].push(tracks);

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

(This is before going back and looking at my code again)
Thank you so much for showing me how to create an empty array. I googled it so many times and in so many different ways and I couldn’t find a relevant answer. So that’s my first thanks. Let me go back and try to fix things now.

So, below is my new code (withing the function). I believe I updated everything you said (bare with me, I’m drowning in self doubt here haha). It still isn’t working. I’ve added an empty array but I’m certain I did it in the wrong spot. Could you mark any errors that you see in my code with those asterisks? Apologies if it seems like I’m slow, but I’m really trying to see and understand my errors so I don’t want to rush and put things in that I don’t understand.

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

You don’t need [tracks] here. “tracks” is your prop so saying collection[id][prop] is the same as saying collection[id].tracks or collection[id][‘tracks’]

And don’t worry about feeling slow at first. When I first got into JavaScript there were many times I felt like I’d never get some concepts, but they eventually came to me. Sometimes it’s good to just take a break and come back with fresh eyes.

I’m kinda surprised this forum doesn’t have syntax formatting in it since it’s for code. There’s probably an issue or two more than just what I mentioned in my last comment, because i also see your curly braces are still missing around the if statement where you create your tracks property. I’m going to load this up in a program that actually formats and try to trouble shoot you with a screenshot from that.

@JaiahAramil. @jprat529 I’ve edited your posts for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

// Only change code below this line
function updateRecords(id, prop, value){
/*We know that we'll delete the prop if value == '' so we start with that
as an if and all other conditions fall under else.*/    
    if(value == ''){
        delete collection[id][prop];
    }
    else{
/*Our next potential for variation is at the value of prop, but we know 
that if it's not == 'tracks', it's pretty straightforward so we get that
out the way here.*/        
        if(prop != 'tracks'){
            collection[id][prop] = value;
        }
/*If prop == 'tracks' we have to do something extra to accommodate
the potential for a non-existent property while leaving the
potential to still add the value to an existent one.*/        
        else if(prop == 'tracks'){
/*The property may sometimes exist so we nest it's creation within another
if statement for the instances where it doesn't.*/            
            if(!collection[id].hasOwnProperty('tracks')){
                collection[id].tracks = [];
            }
/*Whether the tracks property exists or not, we'll always want to push the
value to it, and the property should always be an array so this statement
doesn't need to be nested within the above if.*/            
            collection[id][prop].push(value);
        }
    }
/*We only need a single return statement because we'll always return
collection regardless of the process that takes place above.*/
    return collection;
}

@RandellDawson Thanks for showing me how to format text.

@jprat529 So this sorta gives the answer, but sometimes that’s the only way I really know how to point out issues with code. Hopefully my comments in the code help to explain it though.

function updateRecords(id, prop, value) {
  if (value == ''){
    delete collection[id][prop];
  } else  {
      if (prop !== 'tracks'){
      collection[id][prop] = value;
  }   else if(prop =='tracks'){
    if (!collection[id]hasOwnProperty('tracks')){
      collection[id].tracks = [];
    }
   
      collection[id][prop].push(value);

  }   
  } ```
As far as I can tell I've done everything word for word and it still isn't accepting it. I'm certain it's just a character missing somewhere at this point. Out of all of the lessons on here, this has been the most discouraging.

Actually, I reloaded the page and it worked. I wonder how many times I might’ve had it right and the page just had to be reloaded. I noticed that the “Your text output will go here” wasn’t changing when I clicked submit.

Sometimes its the simple things that are easiest to overlook.

I’m glad you were able to figure it out!