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:

2 Likes

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

2 Likes
// 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.

2 Likes

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

3 Likes

I don’t know how to fix the problem.

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

Because it takes the first if statement first.

1 Like
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.

2 Likes

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.
    "
2 Likes

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

3 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;
  }
}

1 Like

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

1 Like

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?

2 Likes

by writing return object

1 Like

and is your return object always executing?

2 Likes

Only if the If and If else are false.

1 Like

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

2 Likes

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

1 Like

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

2 Likes

But how should it then fix the prop?

1 Like