like what you quoted as
if(object[id][prop] == "")
Ah, yeah, you are not checking the function argument prop
is an empty string there (it’s not undefined because you’ve deleted it, sorry), I assume it’s undefined because of something else you’ve done in the code.
updateRecords(collection, 2548, "tracks", "")
-
object
iscollection
-
id
is2548
-
prop
is"tracks"
if(object[id][prop] == ""){
delete object[id][prop]
console.log("this will be deleted"+ object[id][prop])
}
So
if(object[id][prop] == "") {
Is
if (collection[2548].tracks == "") {
Which is
if ( ['Let It Rock', 'You Give Love a Bad Name'] == "") {
And that isn’t true, so that code block doesn’t run, so nothing gets deleted from there
I’m pretty sure that object[id][prop]
is not an argument of the function
I m totally lost on this. I do not how to break it more down as I do not want to cheat but understand what I am missing or doing wrong, how to check each statement and find out if it is what they are asking me.
here I am trying to do line by line but why do I have undefined? I tried with [prop]
too but it didn’t work and was undefined.
The object with the id 5439
does not have a tracks
or artist
property.
var collection = {
5439: {
albumTitle: 'ABBA Gold'
}
};
const prop = 'artist';
console.log(collection['5439'].tracks); // undefined
console.log(collection['5439'][prop]); // undefined
If value is an empty string, delete the given prop property from the album.
You have a function updateRecords(object, id, prop, value)
.
So for the function call updateCollection(collection, 2548, "tracks", "")
, what is value
?
Please bear with me as I sound really stupid to ask this for a long time and I could see the solution to cheat but I really want to understanding my mistake and lack of knowledge. I haven’t been able to give it time much since a week but I want to restart the problem. the following is my first statement
// Only change code below this line
function updateRecords(object, id, prop, value) {
//If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.
if(object[id].hasOwnProperty(prop) && object[id][prop] != ''){
return object[id][value]
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
I want to find out why is it wrong or what I am doing wrong.
have you tried answering this question?
how do you check for that?
(collection[id].tracks == "")
I was going over the whole thread and wondering isn’t that the way to check if the prop has a value which is empty string e.g in the above code example I am asking
if collection
object has a given id
which has prop
called tracks and if it is equal to ""
(empty string) ?
but isn’t the function parameter that can have a value of empty string?
oh u mean like if (collection[2548].tracks == value)
?
still no
do you understand what the challenge asks you to do?
you beed to change the object
, and how you change it depends on the values of the function parameters
you never check what are the values of the function parameters
let me correct my answer to above if (collection[id].tracks == value) ?
as for ur question if I understand what is being asked, so there r four things being asked.
- If
prop
isn’ttracks
andvalue
isn’t an empty string, update or set that album’sprop
tovalue
. - If
prop
istracks
but the album doesn’t have atracks
property, create an empty array and addvalue
to it. - If
prop
istracks
andvalue
isn’t an empty string, addvalue
to the end of the album’s existingtracks
array. - If
value
is an empty string, delete the givenprop
property from the album.
and how are you checking the values of prop
and value
?
Right, I’m going to write a skeleton here of the thing you’re trying to do. You are having huge difficulty here seeing how the instructions relate to the code you’re supposed to be writing, but the way they are written actually translates directly to code. Maybe this will help. Can you just fill in what goes in the head of each of the if
blocks. I’ve given each one a number, then the thing to fill in is numbered below: can you convert from the description of the condition in English to the condition in JS?
function updateRecords (object, id, prop, value) {
if (/* fill this in with 1 */) {
// Ignore this for now
} else if (/* fill this in with 2 */) {
// Ignore this for now
} else if (/* fill this in with 3 */) {
// Ignore this for now
} else if (/* fill this in with 4 */) {
// Ignore this for now
}
// Your function must always return the entire object.
return object;
}
- If
prop
isn’t “tracks” andvalue
isn’t an empty string, update or set that album’sprop
tovalue
. - If
prop
is “tracks” but the album doesn’t have a “tracks” property, create an empty array and addvalue
to it. - If
prop
is “tracks” andvalue
isn’t an empty string, addvalue
to the end of the album’s existing “tracks” array. - If
value
is an empty string, delete the givenprop
property from the album
function updateRecords (object, id, prop, value) {
if (!object[id].hasOwnProperty(prop) && object[id][prop] !== value) {
//if object with given id does not have the given property AND object
//with given id's prop is not equal to value
} else if (object[id].hasOwnProperty(prop) && object[id].hasOwnProperty("tracks") {
// if object with id has a property (prop) AND object with given id has //own property named tracks
} else if (object[id][prop] && object[id][prop] !== value) {
// if object with given id has prop AND object with given id and prop is //not equal to the value
} else if (object[id][prop] == value) {
// if object with given id and prop equal to value
}
// Your function must always return the entire object.
return object;
}
This is what I would write, is it wrong?
Edit: I have made comment under my condition to show what I have done
well, you are not checking the value of prop
anywhere
and you never check if value
is an empty string or not
I don’t see how the code you wrote checks for those 4 cases. Can you explain how you think that code works?