Solution to Record Collection - missing check an requisite?

Tell us what’s happening: I didn’t understand the Solution 2, to this challenge (see the solution bellow).

Considering that the fourth requirement of the challenge is:

“If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.”

Note: […] and value isn’t an empty string […]

I did not find anywhere in the solution, something that would do this check / validation. Has this requirement been checked?

  **"Solution 2"**

// 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 (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;
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

else if (prop === 'tracks') {
    object[id][prop] = object[id][prop] || [];
    object[id][prop].push(value);
} 

So say the call was updateRecords(collection, 1245, tracks, "Addicted to Love").

So id is 1245, prop is “tracks”, value is “Addicted to Love”.

else if (prop === 'tracks') {
    object[1245]["tracks"] = object[1245]["tracks"] || [];
    object[1245]["tracks"].push("Addicted to Love");
} 

This:

object[1245]["tracks"] = object[1245]["tracks"] || []

Is the same as

if (object[1245]["tracks"]) {
  object[1245]["tracks"] = object[1245]["tracks"];
} else {
  object[1245]["tracks"]  = [];
}

“If tracks exists (ie it evaluates to true) keep its value as-is, otherwise if it doesn’t exist, set its value to an empty array”

Then once that’s done, you know it is an array, and you can push the value to it.

|| is a Boolean OR.

value1 || value2 evaluates to true if one of the two values is true.

It can be used for a short-circuit evaluation, which is a feature of the language that lets || act as a control structure (like if...else) rather than an arithmetic operator, so

var myVar = value1 || value2:

Is saying “assign value1 to the variable myVar unless it is false, in which case assign value2 to the variable myVar”

“If there is already a property "tracks" for this collection entry, then leave it as-is – it’s already an array. If there isn’t, set "tracks" for this collection entry to be an empty array”

Just for reference, JS has three short-circuit operators –

  • || – if the value on the left of the operator is true, do that, otherwise do the one on the right
  • && – if the value on the left of the operator is true, do the one on the right
  • ?? – if the value on the left of the operator is not null, do that, otherwise do the one on the right.
2 Likes

it’s here:

if the first condition is true, that execute and nothing else, if the first condition is false, then the else if stuff is executed and there is no need to check again if value is an empty string or not

2 Likes

Thank you very much, you clarified my doubt, now I got it.

Thank you very much, I also hadn’t understood how the shortcircuit evaluation worked, This clarified a lot!

1 Like