Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/#basic-javascript

Hello All, I need help with this exercise. There are a few parts to this exercise but I guess what’s tripping me up first is how do I access a key that is a number when I thought we could not have keys as numbers, because JavaScript automatically turns those numbers into strings. so first this exercise I know I am suppose to update the record collection however I’m a bit loss and don’t know where to start on this exercise.
I only wrote one line of code for this exercise and I know I’m not even close But I figure why not give what I came up with so far. I m new to the hasOwnProperty method and I forgot I needed a conditional statement and Im pretty sure i need to use that method for this question.

var recordCollection = {
  2548: {
    albumTitle: "Slippery When Wet",
    artist: "Bon Jovi",
    tracks: ["Let It Rock", "You Give Love a Bad Name"],
  },
  2468: {
    albumTitle: "1999",
    artist: "Prince",
    tracks: ["1999", "Little Red Corvette"],
  },
  1245: {
    artist: "Robert Palmer", //album and name of tracks missing
    tracks: [],
  },
  5439: {
    albumTitle: "ABBA Gold", //artist and tracks missing
  },
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
  var records = recordCollection[id].prop.value;         //  <-----my one bad line of code
  return records;
}

// Setup
var recordCollection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
  var records = recordCollection[id]['prop']['value']; //<----- I also tried this way 
return records;
}

updateRecords(recordCollection, 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/90.0.4430.212 Safari/537.36

Challenge: Record Collection

Link to the challenge:

what’s your goal with that one line of code? which part of the instructions you are trying to do? and how that line is going to accomplish it?

Hello,
I have a variable titled recordCollection that has a few objects inside that are basically representing record albums with info such as artist, record names ect. I need to update the information inside with the updateRecords function: since intially posting i have updated my code for the updateRecords function, please take a look below. also beneath the code I will put what the desired results should be. thanks for any help offered!

// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

function updateRecords(records, id, prop, value) {
   if (prop === "artist") {
    recordCollection[prop] = value;
    return records;
  } else if (prop === "tracks") {
    recordCollection.tracks = [value];
    return records;
  }
}

==============================================================
TEST EXAMPLES BELOW

updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);
// in this test result it should add the key and value of artist : ABBA

updateRecords(recordCollection, 5439, “tracks”, “Take a Chance on Me”);
//in this example above i need to add the key tracks as well as an array to hold the value of “take a chance on me”.

Hi ieahleen,

Thanks for any help, I thought I replied to you with my update in code and question however I may have posted a new response in this thread instead of replying directly to you, im not sure either way you should see my update in my question and my code somewhere on this page. I appreciate the help.

can you try answering this for your new code? if you are able to explain what you are doing it’s easier to see how best help you

1 Like

Hi ieahleen, I just finished writing code for this exercise and im getting the output I should get however when I enter my code for the exercise I do not get a passing score. In this excercise I must do the following:
You start with an updateRecords function that takes an object literal, records , containing the musical album collection, an id , a prop (like artist or tracks ), and a value . Complete the function using the rules below to modify the object passed to the function.

  • Your function must always return the entire record collection object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
  • If value is an empty string, delete the given prop property from the album.
    =============================================================
    here is the recordCollection object that I must update with my function updateRecords
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    return (records[id][prop] = value);
  }
  if (records[id].hasOwnProperty("tracks") === false && prop === "tracks") {
    return (records[id][prop] = [value]);
  }
  if (prop === "tracks" && value === "") {
    return (records[id][prop] = value);
  }
  if (prop == "tracks" && records[id].hasOwnProperty("tracks") === true) {
    return records[id]["tracks"].push(value);
  }
  if (value === "") {
    prop === "";
    return (records[id][prop] = value);
  }
  // records[id][prop] = value; //<-----This adds property and value
}

================================================================
With the code I have written for the function updateRecords I am able to get all of these desired outcomes:

updateRecords(recordCollection, 5439, “artist”, “ABBA”); //artistshould be the stringABBA`

updateRecords(recordCollection, 5439, “tracks”, “Take a Chance on Me”); //tracksshould have the stringTake a Chance on Me` as the last element.

updateRecords(recordCollection, 2548, “artist”, “”); //artist` should not be set

updateRecords(recordCollection, 1245, “tracks”, “Addicted to Love”); //tracksshould have the stringAddicted to Love` as the last element.

updateRecords(recordCollection, 2468, “tracks”, “Free”); //tracksshould have the string1999` as the first element.

updateRecords(recordCollection, 2548, “tracks”, “”); //tracks` should not be set

updateRecords(recordCollection, 1245, “albumTitle”, “Riptide”); //albumTitleshould be the stringRiptide`

=============================================================
however even though I get these desired results i still am not passing the excercise. Any help offered is much appreciated, Thanks!

are you doing this?

here you are returing a string

ahh I see thanks so much!! yes i need to update object just by having records[id][prop] = value; and then I must return records;
so like this below


function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
    return records;
  }
  if (records[id].hasOwnProperty("tracks") === false && prop === "tracks") {
    records[id][prop] = [value];
    return records;
  }
  if (prop === "tracks" && value === "") {
    records[id][prop] = value;
    return records;
  }
  if (prop == "tracks" && records[id].hasOwnProperty("tracks") === true) {
    records[id]["tracks"].push(value);
    return records;
  }
  if (value === "") {
    prop === "";
    records[id][prop] = value;
    return records;
  }
  // records[id][prop] = value; //<-----This adds property and value
}

This has gotten me closer!:slight_smile: Thanks, now the only two conditions I am not meeting are :
// running tests After

updateRecords(recordCollection, 2548, "artist", "")

// artist should not be set^

updateRecords(recordCollection, 2548, "tracks", "")

// tracks should not be set ^

These are causing a problem for you. You need to return the entire records object. You are currently returning the return value from the assignment operation, which is the right hand side of the operation.

the code working in that case is this one
what do you need to do in this case?

also this line does nothing:

1 Like

Hi,
I changed the code and now im only failing one condition.

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

The only condition im failing is: updateRecords(recordCollection, 2548, “tracks”, “”)
//tracks should not be set

What’s your current full code?

So i have a nested Object called recordCollection. My code is in the function updateRecords. Im pretty sure I could have written my code better then just using a bunch of if statements but wasn’t sure how else to write this. I will also attach the conditions stated in the exercise that the function must perform which are:

  • Your function must always return the entire record collection object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
  • If value is an empty string, delete the given prop property from the album.
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

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

}

Why this check? I don’t think it is what you meant to write?

Hi,
Yeah your right I dont need that if statement. I just commented it out and everything is working as it should except for the last condition I mentioned earlier. Ive been working on this assignment for a few days and honestly I forgot / dont know why I put that in there :confused:

anybody can help me know why Im still failing this condition? i dont understand when I write this line

delete records[id][prop];

that it will not delete the tracks property. I know tracks is an array, does that matter in regards to the delete key word?

What error are you seeing? What’s your current code?

I suspect you can fix your code (and eventually refactor + simplify) if you just reorder you if clauses)

I believe that this condition is not being met which is : After updateRecords(recordCollection, 2548, “tracks”, “”),tracks` should not be set.

when I run my code I am getting one failed test coming back which is : // running tests
After updateRecords(recordCollection, 2548, “tracks”, “”), tracks should not be set
// tests completed

when I run my code I am seeing that the tracks array which is in id 2548 is not being deleted. I will reattach my full code again.

var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

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

}

ok yea i will sit on this a bit more im sure ill figure it out

Yeah, I took that and reordered the if clauses (only had to move one) and everything passes :slight_smile: