Basic JavaScript: Record Collection alternative code

I have checked the hint and the code is very elegant and short (which is good) but I don’t understand it - parts of the code don’t seem to reflect what we have learnt so far in the course.

Instead I have created the code below which is less elegant and longer but still works when tested. I’d prefer the code to be shorter but thought I’d leave my code here for people to refer to. I think it’s easier to follow as it copies sequence of 4 required functions.

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) {
// 1
if (prop !== "tracks" && value !== "") {
  object[id][prop] = value;
// 2
} else if (prop === "tracks" && object[id].hasOwnProperty("tracks") == false) {
  object[id][prop] = [];
  object[id][prop].push(value);
// 3
} else if (prop === "tracks" && value !== "") {
  object[id][prop].push(value);
// 4
} else if (value === "") {
  delete object[id][prop];
}
return object;
}

updateRecords(collection, 5439, "artist", "ABBA");
console.log(collection);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Hi @CaptainCheckout!

Welcome to the forum!

If you post a full passing solution to a challenge please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Which parts of the FCC solution didn’t reflect what you learned so far? Which parts confused you? When I compare your code to theirs it is pretty much identical except you made two small changes to the first else if statement.

When I looked there was on solution1 code (which is really different and included a shortcircuit evaluation):

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if (value === '') delete object[id][prop];
  else if (prop === 'tracks') {
    object[id][prop] = object[id][prop] || []; // this is called shortcircuit evaluation, see below for explanation
    object[id][prop].push(value);
  } else {
    object[id][prop] = value;
  }

  return object;
}

My code:

// Only change code below this line
function updateRecords(object, id, prop, value) {
// 1
if (prop !== "tracks" && value !== "") {
  object[id][prop] = value;
// 2
} else if (prop === "tracks" && object[id].hasOwnProperty("tracks") == false) {
  object[id][prop] = [];
  object[id][prop].push(value);
// 3
} else if (prop === "tracks" && value !== "") {
  object[id][prop].push(value);
// 4
} else if (value === "") {
  delete object[id][prop];
}
return object;
}

Hi @CaptainCheckout!

FCC provides two solutions to this problem. The solution with the short circuit evaluation is actually solution 2.

In these challenges, the first FCC solution will be the most straightforward one and usually the one most people arrive at on their own. The other alternative solutions can sometimes be more advanced or elegant. You will find this a lot with the algorithm sections too. But it is a great way to learn about different approaches to the problem and compare it to your solution.

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 (’).

to make things clear and avoid anyone thinking of going crazy…

until yesterday there was just one solution, the current solution 2 - I have added yesterday the new solution 1

@jwilkins.oboe @CaptainCheckout

I’m sorry for any confusion

1 Like

Ohh ok cool!

That makes sense. My solution was similar to the first one and I wouldn’t have been able to come up with solution 2 on my own with the short circuit evaluation.

Thanks for the new solution add!

Ok great thanks for clarifying - I did think I was going a bit crazy so that was good :slight_smile: