Record Collection Problem - Not sure what I'm missing

Worked my way through this problem, and wrote the code below. Not sure what Im missing and why this doesnt seem to work. I eventually consulted the Guide and cant seem to make it work.
Everything checks out except the two lines that have (value = “”), but im not sure why its not processing through what I have. Any help and explanations would be great.

My Code:

// Only change code below this line

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

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

    records[id][prop] = value

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

    records[id][prop] = [value]

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

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

  } else if (value === undefined){

    delete records[id][prop];

  }

  return records;

}

Problem Link:

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

If value is an empty string, delete the given prop property from the album.

You’re not checking for an empty string, your checking for undefined.

Thank you for the formatting. Appreciate that.
I also ran it through as (value === “”) , and it still wont pass. Is there anything else?

What is your current code, with the checks for an empty string?

I had the same thought about undefined vs empty string (""), so i toyed with it in the first parts of the else/if statements just to see if they passed, and they do. It is just something with that last portion that doesnt pass, and for the life of me I cant understand why. Ha

Current Code as it stands:

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

  return records;
}

What get’s returned after running the code:

// running tests
After updateRecords(recordCollection, 2548, "artist", ""), artist should not be set
After updateRecords(recordCollection, 2548, "tracks", ""), tracks should not be set
// tests completed

You’re still checking for undefined in other places. If value is an empty string, then either the first or the third condition will be true and you will never get to your final else.

Well that makes sense now. Passed and on to the next lesson. Thank you!

I guess I was assuming in a real application you wouldn’t have entered and empty string, but rather no value at all. This makes sense now in the context of the problem.

It depends. Typically, when we know that a valid value can only ever be one type (string, array, etc) then we keep that value of the correct type even for the “none” type options. In fact, there is an extension of the JavaScript language called TypeScript that is designed to help make sure developers use these best practices. In all likelihood in a “real” case like this (if you aren’t using TS) you would be checking for falsy values, but this challenge is set up with the assumption that value will always be a string.

2 Likes

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