Getting correct answer but still not passed

Tell us what’s happening:
Not able to pass one test even though when I test the code, it runs correctly.

Your code so far



// Setup
var collection = {
2548: {
  album: "Slippery When Wet",
  artist: "Bon Jovi",
  tracks: [
    "Let It Rock",
    "You Give Love a Bad Name"
  ]
},
2468: {
  album: "1999",
  artist: "Prince",
  tracks: [
    "1999",
    "Little Red Corvette"
  ]
},
1245: {
  artist: "Robert Palmer",
  tracks: [ ]
},
5439: {
  album: "ABBA Gold"
}
};

// Only change code below this line
function updateRecords(id, prop, value) {

if(collection[id][prop]==null){
//console.log("NO PROPERTY");
if(prop == "tracks"){
collection[id][prop] = new Array();
collection[id][prop].push(value);}
else{
  collection[id][prop] = value;
}
}

else{
//console.log("YES PROP EXISTS");
if(value!=""){
  //console.log("value is not null");
  collection[id][prop].push(value);
}
else{
  //console.log("Value is null");
  delete collection[id][prop];
}
}
return collection;
}

updateRecords(5439, "artist", "ABBA")
console.log(collection[5439]);



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.

Challenge: Record Collection

Link to the challenge:

yeah I have found the mistake , it is with the if block in the else block
you are checking the value to not be empty but you are not checking if the property is tracks and you are pushing the value into the prop.
it raises an error if the prop is not tracks.
so

if(value!="") //  change this to
if(value!="" && prop == "tracks") //correct

by the way 
console.log("YOU ARE LOOKING BEAUTIFUL"); 

Thank you for your help. It still did not work.

no … I have copied the code and changed the if condition as I mentioned …
it worked for me …
check once again …if it does not work…post the code after change …let me check it
remove all the console.log() statements

Here is the code cleaned up. I think there also might be an issue with the BOLD command not being reachable.


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

oh okay
i got it
in the else block

else delete collection[id][prop]; // change this to below code ...
else if(value == ""){
      delete collection[id][prop];
}

you have to delete the property only when the value is empty…
but in your code it is being deleted when the prop is not tracks
hope it works…

hit a like if it works

1 Like

Thanks, that helped the one error I had for a long time! But now I have 2 different solutions that don’t work. I think I need to look over the logic because I’m getting something confused.

I am happy it worked…
you can message anytime if anything does not work…
happy to help always…

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