Trying about 2 days to solve this challenge!

I got tired of this challenge. Could you help me?:grin:

MY code so far


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

// Only change code below this line
function updateRecords(object, id, prop, value) {
if (prop !== "tracks" && value !== "") {
  return object[id][prop] = value;
} 
else if (prop == "tracks" && object[id][prop] === undefind) {
  object[id][prop] = [];
  return object[id][prop].push(value);
}
 else if (prop == "tracks" && value !== "") {
   return object[id][prop].push(vlue);
 } 
 else if (value === "") {
    return delete object[id][prop];
 }
return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Challenge: Record Collection

Link to the challenge:

What problems are you having? What else have you tried?

I think I have tried as solutions as I am thinking but I didn’t find.
I don’t want to see the solution from “get a hint”.
I want to find answers from myself and get helps from people

debugging is also an important skill to have

and that’s include trying to understand why the code is not doing what you want.

Try to read again the challenge instructions, and the bullet points there, try to read each bullet point and find how you are satisfying it in your code.

There is a thing just at the beginning of the list, once you respect that one, you will be able to see if the rest of your logic works or not

1 Like

The logic of your code is perfectly fine, except you’ve not unterstood what a “return” statement does.

How it should be: Your function receives an object (and some other parameters), then modifies the object with if/if-else statements, and at the end, after all those ifs/elses, it return the modified object.

But you’re returning from the function everywhere, and you’re not returning the object, but whatever is written in your return statements, like here:
return object[id][prop].push(value);
Your function will return only the value that it has pushed into the array, not the whole object.

P.S. You’ve also two typos, you’ve misspelled undefined and value somewhere.

Thank you very much for helping me, you was right !
I remove the return statement from if/else-if statements as told me and I correct some word that I have misspelled like: undefined and value.