Record Collection!

Tell us what’s happening:
Hi. Thought I could do this without asking for help but it’s not working :confused:. I seem to be failing in more ways than I can describe.

  1. Am I accessing my props correctly?

  2. Am I mixing up my id and records parameters?

  3. And after finding the previous challenges on arrays quite easy, I can’t seem to be able to create an array and that’s just demoralizing!

    Your code so far


// 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(id[prop]!=='tracks'&&prop[value]!==''){
records[id]=value;
} else if(id[prop]=='tracks'&&records[id].hasOwnProperty('tracks')==false){
newArray=[].push(value);
} else if(id[prop]=='tracks'&&value!==''){
tracks.push(value);
} else if(id[value]===''){
delete records[prop];
}
return recordCollection;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Record Collection

Link to the challenge:

no, you aren’t.

Try using console.log to see what each of the 4 parameters of the functions are
and then what the value of each expression you are using is
like,

console.log(records)
console.log(id)
console.log(prop)
console.log(value)
console.log(id[prop])
console.log(prop[value])
console.log(records[id])

etc

Hi, I had the same issue.

in the initial “if” condition , you are only checking the parameters of the function , I don’t think you have to access records to check prop and value.

if (prop !== “tracks” && value !== “” )

then to assign the value ; remember records is a collection of objects , you access an individual record with record[id] and you assign the property ‘prop’ to 'value with:

records[id][prop] = value;

I hope it makes sense.

1 Like

The console.log() gives ‘not defined’, unless I did’t understand the instruction here.

While I’m not totally sure about this (not accessing records to check prop and value

I understand this :point_up_2:, and yes, it does make sense.

all of them give undefined? or only some?

All. I kept getting;

ReferenceError: xyz is not defined // where 'xyz' refers to the parameters and my expressions

Are you doing that inside of the function?

No, outside the function

Well, that explains it. The function arguments are not defined by those names outside of the function. The function arguments are only defined by those names inside of the function.

The purpose of that advice is to get you to look at what those four things actually are because the way you are using them is not consistent with the type of data stored in those variables.

Oh, now I get it. The records, for example, is some kind of object, id is a number etc. Now I’ll rewrite my codes, hopefully knowing what the parameters are will help me to use them consistently.

Still can’t pass this after several attempts. I’d like to know if my usage of the given parameters is still inconsistent with the data type of each parameter. Also, is my logic even correct?

function updateRecords(records, id, prop, value) {
/*  console.log(value);
console.log(records);
console.log(id);
console.log(prop);
console.log(prop);
console.log(records[id][prop]);*/

  if(prop!=='tracks'&&value!==''){
    records[id][prop]=value;
  } else if(prop=='tracks'&&id.hasOwnProperty('tracks')===false){
var newArray=[].push(value);
  } else if(prop=='tracks'&&value!==''){
    newArray.push(value);
  } else if(value=''){
    delete records[id][prop];
  }
  return recordCollection;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Here are some comments in your code:

// Only change code below this line
function updateRecords(records, id, prop, value) {
  console.log(id); // Is 'id' an object?
  console.log(id.hasOwnProprety('tracks')); // Can 'id' have a tracks array?

  if (prop !== 'tracks' && value !== '') { 
    // This is good - you are *updating* the value of the property
    records[id][prop] = value;
  } else if (prop == 'tracks' && id.hasOwnProperty('tracks') === false) { // You should use === over ==
    // You should not declare a new variable here
    // You need to *update* the appropriate property
    // "If prop is tracks but the album doesn't have a tracks property,
    // create an empty array and add value to it.""
    // This empty array should be for the tracks property!
    var newArray = [].push(value);
  } else if (prop == 'tracks' && value !=='') {
    // And here you need to 'push' onto the tracks property array
    newArray.push(value);
  } else if (value = '') { // = is not ===
    delete records[id][prop];
  }
  return recordCollection; // You should not reference the global variable
}
2 Likes

I’ve tried editing my codes but I’m still falling short, and I can’t seem to find the error. When I run my code, I pass 5 out of the 7 ‘checklists’.

function updateRecords(records, id, prop, value) {
  if(prop!=='tracks'&&value!==''){
    records[id][prop]=value;
  } else if(prop==='tracks' && records.hasOwnProperty('tracks')===false){
    records[id][prop]=[value];
  } else if(prop==='tracks' && value!==''){
records[prop].push(value);//pushing correctly?
  } else if(value===''){
    delete records[id][prop];
  }
  return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Vs

Typo perhaps?

this is always false, do you understand why?

Because the tracks property isn’t actually a property of records, unlike id. So I could have;

console.log(records[id]);

…while:

console.log(records[prop]);

…is undefined

Did I get that right?

correct! are you able to make it right?

I think so. I suppose that’d be;

...else if(prop==='tracks' && records[id].hasOwnProperty('tracks')===false)

?

that’s much better

how’s the whole algorithm coming along? what do the tests say?