My all test expect one test is failing and I don’t understand what I am missing? Already it took me days to solve this but now I m stuck and do not know what to do, the first element in 2468’s tracks is 1999 but the following messsage
" After
updateRecords(collection, 2468, "tracks", "Free")
tracks should have the string 1999 as the first element. // tests completed"
// 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) {
for(let obj in object){
if(!object.hasOwnProperty("tracks") && object[id]["tracks"] !== ''){
object[id][prop] = value;
}
if(object[id][prop] !== "tracks" ){
object[id]["tracks"] = []
object[id].tracks.push(value)
}
if(object[id]["tracks"] !== ""){
object[id].tracks = []
object[id].tracks.push(value)
}
if(object[id][prop] == ""){
delete object[id][prop]
}
return object;
}
}
updateRecords(collection, 5439, 'artist', 'ABBA');
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.
Take a look at what function returns for this case, it should help with figuring out what is happening. You can add console.log call at the bottom of the code and see the result.
First of all, you don’t need that for loop. You’re returning in the first iteration anyway, it’ll only run once (and you don’t need a loop for this challenge).
Now for your problem, this is the failing test: updateRecords(collection, 2468, "tracks", "Free")
If you run that, three of your if-conditions are true:
First if: if(!object.hasOwnProperty("tracks") && object[id]["tracks"] !== '')
If you log the object, it has no property “tracks”, and object[2468]["tracks" ] isn’t an empty string, but it’s an array with two songs in it. So at this point, you’re updating the object the first time, and you set its tracks property to Free, thus overriding the array that was there before: tracks: 'Free'
Second if: if(object[id][prop] !== "tracks")
If you log the object, object[2468]["tracks" ] is not equal to "tracks", it’s equal to "Free" (because of what you did before). Now you set the tracks property to an empty array and push "Free" into it.
Third if: if(object[id]["tracks"] !== "")
object[2468]["tracks" ] is now an array with one item "Free" in it, so it’s not an empty string. Once again, you set the tracks property to an empty array and push "Free" into it.
This challenge is a tricky one. Just remember that object[id]["tracks"] should always be an array, never a string.
You should be able to click on the triangle on the left and see more. However if you add that to the code on freeCodeCamp, console output should be displayed in the frame that says “Your test output will go here”.
I’ve added spoiler tags to your solution. Can you please not just post solutions in future, instead please try to help guide the OP towards the solution. If the OP wanted the solution, they can see that from the challenge itself, you do not also need to post it here.