Basic JavaScript - Record Collection

Tell us what’s happening:
Hello I am trying to solve this problem in that i have to make a function who can edit details in an object nested with object and arrays this is I wrote the code so far and now all calls are passed and i can’t undestand what i do this is the condition is not passing=>updateRecords(recordCollection, 5439, “tracks”, “Take a Chance on Me”)
in this the recordCollection[5439][tracks] not have a key or value do you please tall me what is wrong with my code??

Your code so far

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

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Let’s check your code and see if it meets the requirements:

If value is an empty string :exclamation:

if(value === "" && prop !== ""){ 

It checks the 'value', but you are also verifying the 'prop' (when in reality they don’t ask for it).
Change it to:

if(value === ""){  -> Like this, it will check only for value

If prop isn't tracks and value isn't an empty :white_check_mark:

else if ( prop !== "tracks" && value !== ""){

If prop is tracks and value isn't an empty string :exclamation:

else if ( prop === "tracks"){

The last condtition, verifies if the prop is 'tracks', however it doesn’t check if the value is empty. (Fix this, even though is not the main problem).

The main problem is located here:

if (value !== "" && records[id].hasOwnProperty('tracks') === true){
      records[id][prop].push(value);
    }
    }
else if (value !== "" ) {
      records[id][prop] =[];
      records[id][prop].push(value);
   }

It appears that this conditional:

value !== "" && records[id].hasOwnProperty('tracks') === true

is going to verify that 'value' is not empty and 'tracks' exists as property using a specific id.

updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me")

Using the id '5439', you will notice that inside the array 'recordCollection' it doesn’t have the 'prop' 'tracks'

So, you need to initialize the prop and afterwards save it

      records[id][prop] =[]; -> should be  records[id]["tracks"] = [];
      records[id][prop].push(value);

You already wrote the logic for it, however it is not going to work, try to relocate it inside the else if ( prop === "tracks"){ condition

Ask again if you get stuck

Cheers

thanks you save the day

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.