Stuck on record collection code

I am only getting the first and last check. I am unsure what is wrong with my code.

Also towards the end it is forcing me to use brackets to avoid error messages for using the dot construction.

  **Your code so far**
  Below is what I have inside my function:
if (value==""){
  delete records.id.prop; //Delete prop from album for empty string value
} else if (prop=="tracks") {
    if (records.id.hasOwnProperty("tracks")){
      records.id.prop.push(value);//Add value to tracks array 
    } else {
      records.id.prop=[]; //create empty tracks array
      records.id.prop.push(value);//Add value to tracks array 
    }
  } else { 
      records[id][prop]=value;//update prop to value
  //  records.id.prop=value; Why do I have to use brackets to avoid error from this line.
    }
  return records;
  **Your browser information:**

I am in Windows 11 on the Brave browser

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

You need to wrap your code in triple back ticks. To display your code in here you need to wrap it in triple back ticks. On a line by itself type three back ticks. Then on the first line below the three back ticks paste in your code. Then below your code on a new line type three more back ticks. The back tick on my keyboard is in the upper left just above the Tab key and below the Esc key.

You can’t use dot notation when the property name is stored inside of a variable

Here is my code (the inside of the function).

if (value==""){
  delete records.id.prop; //Delete prop from album for empty string value
} else if (prop=="tracks") {
    if (records.id.hasOwnProperty("tracks")){
      records.id.prop.push(value);//Add value to tracks array 
    } else {
      records.id.prop=[]; //create empty tracks array
      records.id.prop.push(value);//Add value to tracks array 
    }
  } else { 
      records[id][prop]=value;//update prop to value
  //  records.id.prop=value; Why do I have to use brackets to avoid error from this line.
    }
  return records;

I don’t understand what this means or why the last line is the only one where this is relavant.

it’s relevant everywhere

if you write records.id you arr accessing the proeprty literally named id. Instead you need to use the value inside the variable

Here is a more cleanly commented version of my problematic code:

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

if (value==""){
  delete records.id.prop; //Delete prop from album for empty string value
} else /*!value==""*/ if (prop=="tracks") {
    if (records.id.hasOwnProperty("tracks")){
      records.id.prop.push(value);//Add value to tracks array 
    } else /*!records.id.hasOwnProperty("tracks")*/ {
      records.id.prop=[]; //create empty tracks array
      records.id.prop.push(value);//Add value to tracks array 
    }
  } else /*!prop=="tracks"*/ { 
      records[id][prop]=value;//update prop to value
  //  records.id.prop=value; Why do I have to use brackets to avoid error from this line.
    }
  return records;
![Errors|690x181](upload://47q2uItSoI6XoC87tF0n47oNrxm.png)

}

How can I upload a screenshot of my error messages. Here is a google drive link:

Do you see a property named id anywhere in the records object?

As others have already tried to explain, you can not use dot notation with variables.

I strongly suggest you review the following challenges:

you still can’t use dot notation with variables

1 Like

Code works now:

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

if (value==""){
  delete records[id][prop]; //Delete prop from album for empty string value
} else /*!value==""*/ if (prop=="tracks") {
    if (records[id].hasOwnProperty("tracks")){
      records[id][prop].push(value);//Add value to tracks array 
    } else /*!records.id.hasOwnProperty("tracks")*/ {
      records[id][prop]=[]; //create empty tracks array
      records[id][prop].push(value);//Add value to tracks array 
    }
  } else /*!prop=="tracks"*/ { 
      records[id][prop]=value;//update prop to value
  //  records.id.prop=value; Why do I have to use brackets to avoid error from this line.
    }
  return records;
}

Thx everybody!!! :upside_down_face: :pray: :+1:

:+1:t5:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.