Record Problem Java

I am working on RECORD COLLECTION Java problem.

   **Your code so far**
       if (prop !=="tracks" && value !== "" ){records[id][prop]= value;} 
 else if (prop !=="tracks" && value === "" ){delete records[id][prop]}
 else if (prop ==="tracks" && value === "" ){delete records[id][prop]}
else if (prop === "tracks" && records[id].hasOwnProperty("prop")) {records[id][prop].push(value);}
else if (prop === "tracks" && value !==""){records[id][prop]= [value];}

The message I get
// running tests After

updateRecords(recordCollection, 2468, "tracks", "Free")

,

tracks

should have the string

1999

as the first element. // tests completed

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

 return records;
}

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

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

Challenge: Record Collection

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you for letting me know!

Can you help me with the problem?

this is JavaScript, not Java - Java is a completely different language

what property are you checking for here?

I was checking that tracks were there for the object. So if there are items in tracks you just push one more in. Thanks for helping!

is this how you check that?

I don’t know, I’m learning. This was a suggestion on the hints also I saw someone else using it. That line of code seems to work. It is when tracks is set to that I can’t get it it to work.

your issue is that records[id].hasOwnProperty("prop") is always false
it is what I am trying to make you reflect on - what property are you actually testing? not what you want it to be testing, what it is actually doing. Read it carefully, what is it doing?

Ok ! I’ll try again but I’ve been stuck on this for a few days so if you can give me more hints I’d appreciate it!

records[id] does not have a property literally named "prop", right? so you need to write something different on what property name to test

So I test “tracks”? I took out the hasOwnProperty and again everything works except for when the object does not have an array for the tracks.

you need to test if a certain property is present or not before being able to do a step, so you can’t remove hasOwnProperty completely

Tell us what’s happening:
Can you tell me why the code does not work. I’ve spent too much time on this and I can’t figure it out. I need direct instruction please.
This is the error message
// running tests After
updateRecords(recordCollection, 2468, “tracks”, “Free”)
tracks should have the string 1999
as the first element. // tests completed

Please help !

   **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 (prop !=="tracks" && value !== "" ){records[id][prop]= value;} 
else if (prop !=="tracks" && value === "" ){delete records[id][prop]}
else if (prop ==="tracks" && value === "" ){delete records[id][prop]}
else if ( prop === "tracks" &&  records[id][prop] !==[] && value !== "")
{records[id][prop]= [value];}
else if (prop === "tracks" && records[id][prop] ===[] && value !== "")
{records[id][prop].push(value);}
 return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
   **Your browser information:**

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

Challenge: Record Collection

Link to the challenge:

I have formatted your code for readability.

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop !== "tracks" && value === "") {
    delete records[id][prop]
  } else if (prop === "tracks" && value === "") {
    delete records[id][prop]
  } else if (prop === "tracks" && records[id][prop] !== [] && value !== "") {
    records[id][prop] = [value];
  } else if (prop === "tracks" && records[id][prop] === [] && value !== "") {
    records[id][prop].push(value);
  }
  return records;
}

I’m not sure why you duplicate the delete case?

You cannot check for an empty array like that. What happens if you try

console.log([] === [])

Also, if the property is undefined, it will not be equal to an empty array (even if you could check for an empty array like that).

How do you check if an object has a property?

This seems like a logic error. You are basically deleting records[id][prop] whenever value === "" regardless of what the value of prop is. Was this your intention?

  1. Thank you for formatting it for me. I’ll do better next time.
  2. I have to deletes one for when prop is tracks and one for if prop is not tracks.
    3.) I had hasOwnProperty(tracks) to check for but it still gave me one error message.
    4.) So I really need a lifeline here. I have tried everything within my limited knowledge and either it says it cannot add the track to ABBA or 1999 is not the first track in 1999. I don’t get this because i push the value to the array it should remain first.
    I tried ===[ ] out of desparation not logic :slight_smile:

if the prop is tracks and the value is blank you delete tracks value
If the prop is album or artist and the value is blank you delete the album or artist
Isn’t that what is supposed to happen?

I am not sure what console.log ( ===) does?

yes, but you delete using prop so it is not necessary to check if prop is "tracks" or not
you just delete when value is an empty string

I have not seen this version of your code. It’s better than what you have now

it just prints false, you can’t use it to check if an array is empty, or if an array exist

1 Like