Record Collection- the double 'if'

I managed all bar one of the elements but when I went to the solution, the very first section of the solution involved a ‘double if’ for want of a better term ie

  if (prop === "tracks" && value !== "") {
   if(collection[id][prop]) {

What’s going on here? Why can’t you use ‘else if’ instead of the second ‘if’?

how would you write the logic with else if ? (just so I don’t answer a question that you never meant to ask).

In my head, the closest I can get to explaining the ‘double if’ is as like an’ if’ followed by an ‘else if’. However, I know that’s wrong, I just don’t know why.

So, if I was re-writing the above with an else if, it would end up looking like:

if (prop === "tracks" && value !== "") {
   }  else if(collection[id][prop]) {

but now I’ve got myself into a complete muddle… Why do the two ‘ifs’ nest?

ok i think you’re missing a big part of the logic here.

if (my son goes to school today) {
  if (his teacher shouts at him) {

I need two ifs above because the part about the teacher shouting is not logical unless my son goes to school first…

In the code you gave, it is actually saying this:

if ( the  caller want to modify the tracks and has given me something new to add to the tracks ) {
  if (my cd already has a tracks array) {
    //then go ahead and add the new stuff to the existing tracks array
 } else {
     // I don't have a tracks array so I can't add until I make one
    // make a new tracks array
    // now add the new stuff to it

So if you used else if to do this, you would be creating a tracks array when the caller did not ask you to add new tracks (and why would you want to do that?)

ps. I don’t really like this code. I’d rather do it slightly differently myself but anyway, just to answer you…


Ah. I see now. Sort of blind sided me as we hadn’t covered it in the lesson and I obviously wasn’t seeing the logic in order to google it…Many thanks! I’ll go back to the lesson and take it apart with what I now know before I move on to the next lesson.

1 Like