Basic JavaScript - Record Collection - Where did I do wrong?

Hi everyone,
I am a beginner and I am working on this challenge!
Sorry I am not allowed yet to post link on here but you can type in search
“Freecodecamp, basic-javascript, record-collection”
Here below is my code but it did not work:

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

When I ran the code it kept saying:

Cannot read property ‘artist’ of undefined

Cannot read property ‘tracks’ of undefined
etc.
Would you please help explaining to me where did I do wrong and why?
Thank you!!!

Hello camper , the first test says :
If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.
so you have to use logical operator && (wich means and ) so you can type :
if (prop !== “tracks” && value != “”){ some code} .

Thanks M! May I ask does my code wrong in term of logic? coz I am still a bit confused!
If the requirement is if A=B && C=D => x else if A=B && C!=D => y
Can’t I check if A=B first then continue to check C rather than checking A & C at the same time?

if (something is true) { some code}; // this is the first condition
else if ( something is true) {some code} // if the first condition is false run this second condition
you can type as mush as you need of else if statement
else { some code } // if all the conditions are false . use this condition wich is the last .

2 Likes

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 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.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Thank you! Sorry about that!