Record Collection Problem - 2 questions

Hi everyone,

I was hoping someone could clear this up for me (in reference to the answer key for the problem):

  1.  Why are quotes around <code>"tracks"</code> required to pass the test? I'm not understanding why they're necessary.
    
  2.   Why is including <code>return</code> after each <code>if else / if</code> statement incorrect? I'll copy and paste my code here just to make it clear what I'm referring to.
    
// Only change code below this line

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

  if (prop !== "tracks" && value !== "") {

      return records[id][prop] = value;

  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {

      return records[id][prop] = [value];

  } else if (prop === "tracks" && value !== "") {

      return records[id][prop].push(value);

  } else if (value === "") {

    delete records[id][prop];

  }

  return records;

}

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 (’).

2 Likes

Thanks for the information. I saw how bad it looked and was trying to fix it but you beat me haha.

1 Like

The return statements like that don’t work and you shouldn’t use them in that way.

The assignment operator = has a return value. You can see this by doing

let myVar = 4;
console.log(myVar = 5); // will log the 'return value' of the assignment 'function'

This seems funny, but it lets you do things like

let myFirstVar = 4;
let mySecondVar = 5;
myFirstVar = mySecondVar = 6; // will set both to 6

So in this case, the right hand side of the assignment operator becomes your return value for the entire function.


As far as the quotes go, "tracks" is a string and tracks is an undefined variable. In this challenge, the prop is a string holding the property name, so you need to compare it against a string with the property that you are interested in.

2 Likes

Got it. Thanks for clearing that up!

1 Like

check the first requirement in the bullet point list again

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