Record Collection -- I'm stuck

Tell us what’s happening:
After many hours of research and rewriting, I still can’t get this to work and I’m not sure why. I’d love any hints that folx are willing to share. Thank you! The error message I’m getting is below. I thought maybe my logic sequence was leaving out a particular use case but after writing it out several times in plain english, it still seems ok to me. I’m not sure where I’m going wrong. TIA!

Error message: << After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"), tracks should haveTake a Chance on Me as the last element.>>

Your code so far


// Setup
var collection = {
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(object, id, prop, value) {

if (value === ''){
delete object[id][prop];
}

else if (prop != "tracks") {
object[id][prop] = value;
}

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

else {
  collection[id]["tracks"] = value;
  }

return object;
}

updateRecords(collection, 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/86.0.4240.111 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

The tracks property should always be an array. In this block, it is set to a string.

also, to add to what Ariel said, why are you suddenly using collection here?

1 Like

Because I’ve just been trying everything I can think of because I still don’t have a super clear understanding of when to use brackets vs not.

Thanks! I changed “collection” back to “object” and removed the quotes around tracks inside the brackets and I still get the same error.

Please include your new code. I’m not sure where you removed quotes, but I don’t think that was the problem. The problem was in the assignment that both @ilenia and I quoted.

Thanks so much. Here is the new code:


function updateRecords(object, id, prop, value) {

if (value === ''){

  delete object[id][prop];

  }

  

else if (prop != "tracks") {

  object[id][prop] = value;

}

else if (prop === "tracks" && object[id].hasOwnProperty("tracks")) {

object[id][prop].push(value);

}

else {

  object[id][prop] = value;

  }

  return object;

}

value is a string, so this will set tracks equal to a string instead of an array.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks for the formatting tips ArielLeslie - I am new to fCC so wasn’t sure how best to paste the code so that’s helpful! I changed value to be an array in both spots but am now getting two errors. :laughing: :cry:

function updateRecords(object, id, prop, value) {

if (value === ''){
  delete object[id][prop];
  }
  
else if (prop != "tracks") {
  object[id][prop] = [value];
}

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

else {
  object[id][prop] = [value];
  }

  return object;
}```

only tracks need to be an array

1 Like

Thanks both of you! Only took a total of about 6 hours (seems like a lot but I’m new to this so what do I know?) but I FINALLY got it to pass. Here is my finished code in case it’s helpful to anyone in the future. My issue was that I wasn’t creating the empty array before assigning the value to it. I thought it could be done with this line:

object[id]["tracks"] = value;

I’m still not clear on why that wouldn’t work but onward I guess. Thanks for your help!

if (value === ''){
  delete object[id][prop];
  }
  
else if (prop != "tracks") {
  object[id][prop] = value;
}

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

else {
  object[id]["tracks"] = [];
  object[id][prop].push(value);
  }

  return object;
}

Good job figuring you out. Let me see if I can help you understand why your old code didn’t work.

let val = "sagraham541";
let stringval = val; // stringval is now "sagraham541"
let arrayval = [val]; // arrayval is now ["sagraham541"]
let pusharr = []; // pusharr is an empty array
pusharr.push(val); // pusharr is now ["sagraham541"] 

Thank you so much ArielLeslie! We haven’t learned val yet but I have a friend who is going to walk me through your notes tomorrow. I hope someday I’m competent enough to be of help to someone else the way you have been to me! :slightly_smiling_face: