Hi all, I developed the code for this challenge using repl.it, tested it against every test. It is now working in repl.it
I copied the code into the fcc editor (bonfire?) and then my troubles began:
The code no longer passes the tests where the property should be deleted because the input value argument is empty.
- After updateRecords(2548, “artist”, “”), artist should not be set
- After updateRecords(2548, “tracks”, “”), tracks should not be set
It doesn’t make sense to me…
- I have checked the delete stmt in my code: it returns true
- I have console checked the relevant collection entry just before the return, and the properties have indeed been deleted. Still, the tests fail.
- I also tried it in jsfiddle - and there it works as required !!!
Here’s my code:
// Keep a copy of the collection for tests
var colCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (colCopy[id][prop] !== undefined) // property exists
update();
else
create();
function update() {
if (value === "") {
delete colCopy[id][prop]; // <---- THIS RETURNS TRUE
} else {
if (prop === "tracks")
colCopy[id][prop].push(value);
else
colCopy[id][prop] = value;
}
}
function create() {
if (value !== "") {
if (prop === "tracks") {
colCopy[id][prop] = [];
colCopy[id][prop].push(value);
} else {
colCopy[id][prop] = value;
}
}
}
if (id == 2548) console.log(id, prop, value, colCopy[id]); // <---- THIS SHOWS THE PROPERTIES ARE GONE
return colCopy;
}
updateRecords(5439, "artist", "ABBA");
Can anyone help me find out what I am doing wrong here?
Thanks in advance.
Rainer