Basic JavaScript: Record Collection ( Got stuck)

Tell us what’s happening:
I have read the instructions many times and today read the hint but still couldn’t understand.

Please give instructions for completing this one.

Your code so far


// Setup
var collection = {
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(object, id, prop, value) {
return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Record Collection

Link to the challenge:

1 Like

What have you tried so far? What part of the instructions is confusing you?

1 Like
// Setup
var collection = {
  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(object, id, prop, value) {
  if (object[id][prop] !== "") {
    return object[id][prop] == value;
  } else if (object[id].hasOwnProperty(tracks) && object[id][prop] !== "") {
    return object[id].push(tracks)
  } else if (object[id][prop] == "") {
    return delete object[id][prop];
  } else {
    return object;
  }
}

updateRecords(collection, 5439, 'artist', 'ABBA');

I don’t understand what I am doing wrong, and the instructions is hard to understand I think.

I think you need to consider carefully the order of your if statements. Only the first if clause that is true will run.

2 Likes

I don’t know how to fix the problem.

if (x < 5) {
  console.log("x is less than 5");
 } else if (x < 3) {
  console.log("Why will this clause never run?");
}
1 Like

Because it takes the first if statement first.

let x = -2;

if (x < 5) {
  console.log("x is less than 5");
 } else if (x < 3) {
  console.log("Why will this clause never run?");
}

A specific value of x isn’t the issue. What happens when you try this code?


Edit: exactly! This is what’s causing your code problems.

1 Like

How do you follow these instructions?

"

  • Your function must always return the entire object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
  • If value is an empty string, delete the given prop property from the album.
    "

you can do them in order, making the if statements mirror each bullet point, plus the first one that says you always must return the entire object - the return object that was the last line of the function when starting satisfy that if you do not add other return statements

your first if statement is value != "" and then one only thing happen there, but your code should be able to do different things for that, one if prop is tracks and one if it isn’t

2 Likes

My new code still not working

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

I followed the instructions and I didn’t understand what they meant.

let’s work on them one by one:

return the whole object

you have many many returns statements that do not return the whole object

how do you return the whole object?

1 Like

by writing return object

and is your return object always executing?

1 Like

Only if the If and If else are false.

so what do you need to change to make it always execute?

1 Like

Either have all if and if else statement false or have return object on every if and if else statements.

or remove all return statements and have a single return object after the if/else if/else chain so it is executed no matter what happens inside the chain

1 Like

But how should it then fix the prop?