So I’ve thankfully managed to fulfil most of the criteria of the challenge (thanks to Hint 3) EXCEPT for one: " After updateRecords(recordCollection, 2468, "tracks", "Free") , tracks should have the string 1999 as the first element."
After doing a console.log, I can see that tracks: [Free] has been pushed into the array, but i don’t understand why I have somehow overwritten the array instead of adding to it… I can’t seem to find a similar forum post hence, sorry for the trouble
// Setup
var recordCollection = {
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(records, id, prop, value) {
if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
} else if (prop === "tracks" && records[id][prop] !== "tracks") {
records[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
if (records[id].hasOwnProperty(prop) === true) {
records[id][prop].push(value);
}
}
if (value == "") {
delete records[id][prop];
}
return records;
}
console.log(updateRecords(recordCollection, 2468, "tracks", "Free"));
Ah alright so I thought I was checking if the record collection had a “tracks” property but what I was actually checking was if there were any properties named “tracks”… I think, lol.
So redid that portion of the code and the error got transferred to the “hasOwnProperty” function instead so i’m guessing my nested if to check for the hasOwnProperty method isn’t working as it should:
// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
} else if (prop === "tracks" && records[id][prop] === "") {
records[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
if (records[id].hasOwnProperty(prop)) {
records[id][prop].push(value);
}
}
if (value == "") {
delete records[id][prop];
}
return records;
}
the two things you say here are actually the same, and none of the two is what was happening
you were checking if the value of record[id][prop] is different from "tracks", which is always true
I’m still having trouble understanding why my nested if to check the hasOwnProperty condition doesn’t seem to work to add the value to the end of the array. It seems to make sense to me based on the requirements of the question but I must be missing something obvious, would appreciate your inputs, thanks!
function updateRecords(records, id, prop, value) {
if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
} else if (prop === "tracks" && records[id][prop] === "") {
records[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
if (records[id].hasOwnProperty(prop)) {
records[id][prop].push(value);
}
} else if (value === "") {
delete records[id][prop];
}
return records;
}
console.log(updateRecords(recordCollection, 2468, "tracks", "Free"));
I’m thinking that it checks if there are any properties in the collection… but now i’m starting to realize that the question is specifically asking for if there are any “tracks” properties in the first place… thanks so much for the nudge Jeremy! lemme tweak things a bit!
Thank you Jeremy and Ilena! I realised I mixed up challenge statements 2 and 3 and I should have been checking for the “tracks” property availability in my second ifelse statement. All good now and I’m happily moving forward!