Record Collection Comparison Operators

Record Collection Comparison Operators
0

#1

Tell us what’s happening:

I’m having issue with the Record Collection, but my main concern right now is what the issue is with the operators.

The first line of if/else necessitates that I use the assignation operator or I don’t pass one of the tests.

As far as the lines of code below this, I am not sure if it makes a difference if I use the the double or triple =. Neither of them pass and I remain stumped for right now, but I have an inkling that I should use exact equality. Could someone confirm or correct this assumption?

Your code so far


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
if(prop = "tracks" && collection[id] != value){
  [value] = [];
  collection[id][prop] = value;
}
else if(prop === "tracks" && value !== ""){
  collection[id][prop].push(value);
} 
else if(prop === "tracks" && value === "") {
  delete collection[id][prop];
}


  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/


#2

Stick with === instead of ==.

An example of when you would use == instead of === would be if you were comparing values could be either numbers or strings of numbers and wanted to consider a string of numbers the same way as an actual number.

For example, in the following code, I have an array of elements (numbers and “number” strings).

var arr = [1, 2, "3", 4, "5", 6, "7", 8, 9, "10"];

If was iterating through arr and wanted to check if an element was equal to the number 5 regardless if it was the actual number 5 or a string containing only 5, then I would use the following comparison:

for(var i=0; i < arr.length; i++){
  if (arr[i] == 5) {
    console.log('found a 5');
  }
}

The above will only display ‘found a 5’, because I used the == operator instead of the === operator.


#3

I am stuck on two last aspects of this challenge.
The first has to do with how I am using .push()
It seems from that I am not defining value, but I am not sure if this assumption.
The second has to do with setting a value for “artist”. I have not attempted to do so so I’m confused as to how I’ve done this and would like a little guidance as to how I’ve managed to do this.


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


#4

In the test case of updateRecords(5439, “tracks”, “Take a Chance on Me”), the following else if evaluates to true, so you attempt to push a value into a “tracks” property. The push method can only be used on an array, but id #5439 does not have a tracks property at this point, so it is definitely can not push any value to it. You need to create an empty array if one does not already exist per the instructions (seen at bottom of this reply).

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

Per Challenge Instructions:

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.


#5

Part of my problem is that I can’t figure out which line of code corresponds to which record. I think I am writing for one line, which seems to satisfy what another line needs.

This problem is currently focused on #5439. The first line was what I wrote in an attempt to solve #5439, but it does not address this problem and I am having difficult figuring out where to proceed from here because this is the only code I can conceive as solving this particular issue.


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
if(prop !== "tracks" && prop !== "artist") { //5439??
 value == [];
 collection[id][prop] = value;
} else if(value === "") {
  delete collection[id][prop];
} else if(prop === "tracks" && value !== "") {
  value === []; // 1245
  collection[id][prop].push(value);
} else if(collection[id].hasOwnProperty(prop)) {
  value === [];
  collection[id][prop].push(value);
}
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


#6

You need to step back and stop trying to band-aid your code to pass a specific test case/record. Read over the instructions as many times as it takes and write out your algorithm in plain language first (no code). You algorithm should reflect the steps needed to meet requirements of the instructions. The tests cases are just to validate you have followed the instructions.


#7

Alright, a lot of soul searching and vision questing and all and I’m still at the same impasse. Therefore, I ask for a bit guidance or correction of my logic.

Basically, it is my final line of code below. I have used all suggestions and directions and I am perplexed by how I am left with two records which only need new “tracks” to be pushed to end. What I have done is to define the id and prop as the value and define the value as “tracks”. Then, to use that definition to push “tracks” onto the value and, thereby, nudge to the end of the elements.

I don’t think this is logic is sound; yet, I’m not exactly sure what I’m missing but I am indeed missing something.

Queries are:

  1. Should I be using the === in this case or should I simply be assigning value to collection[id[prop] ?
  2. Should I be writing value and “tracks” in a manner that acknowledges the array type of “tracks”? (E.g. value.tracks ??)

Much obliged for any feedback.

Later.


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(collection[id][prop] === value) {
    collection[id][prop][value]; 
  } else if(value === ""){
    delete collection[id][prop];
  } else if(prop === "tracks" && prop !== value) {
    value === [];
    collection[id][prop][value];
  } else if(prop === "tracks" && value !== "") {
    value === [];
    collection[id][prop].push(value);
  } else if(prop !== "tracks" && value !== "") {
    value === "tracks";
    collection[id][prop] = value;
  } else if(collection[id][prop] === value && value === "tracks") {
    collection[id][prop].push(value);
  }
  
 return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


#8

This else if condition above will always evaluate to true. What situation are you seeing that you think you need this part to solve this challenge?


#9

I can’t seem to .push “tracks” . “tracks” is my prop and I have created an empty array for value and pushed that value onto the end of the value of “tracks”. I’m not sure where I’m going wrong.

Is it it a syntax thing? Have I done something wrong there?

OR, am I neglecting an element in the code?

OR, is something somewhere else in the code preventing my .push method from carrying through?


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(collection[id][prop] === value) {
    collection[id][prop][value]; 
  } else if(value === ""){
    delete collection[id][prop];
  } else if(prop === "tracks" && value !== "") {
    value === [];
    collection[id][prop].push(value);
  } else if(prop !== "tracks" && value !== "") {
    value === "tracks";
    collection[id][prop] = value;
  } 
  
 return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


#10

What do you think the code above is doing?


#11

It’s defining for a condition where prop is “tracks” and value is not empty. I want to push something onto the end of “tracks” and I need an array to push that onto.

I’ve been messing around with the code. What I supposed was wrong was that value didn’t need to be a new array because value already had something in it (e.g. value !== “”). So, I wondered if prop needed an array since it equaled “tracks” but did not have the available space to input something into it. This seems flawed to, but I’m stuck as to where I’ve got myself caught. Below is what I’ve come up with so far.

else if(prop === “tracks” && value !== “”) {
prop === [];
collection[id][prop].push(value);
}

#12

Please use 3 backticks on the line before and after any pasted code, so that it properly formats in your post. I have added the backticks.

You are not assigning anything to prop in the above line, because === is not the assignment operator. Also, you should not be trying to assign new values to any of the function arguments (i.e. prop or value), because you them to be their original values when comparing them to applicable property/value of each object in the contacts array.


#13

I don’t know what I’m doing wrong here.
I think I am creating an empty array so that I can push “tracks” to the end. I’ve re-checked it several times and looked around at other Help posts and, as far as I can tell, I’m doing the same thing that others are doing but their code is working and mine is not. The proper approach is flying right over my head. I know the key is to do with defining “tracks”; yet, I’ve tried this by

prop === "tracks"

as well as

collection[id][prop].push("tracks");

collection[id]["tracks"].push(value);

I’m really perplexed here. Is there something earlier in my code that’s stopping it from working correctly?



var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line

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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


#14

prop is a variable (argument) containing a string value passed into the function. You don’t want to change this variable. You want to assign an empty array to a property named “tracks” inside the current contact object IF the “tracks” property does not already exist.

FYI - Like I said before === is not the assignment operator. The assignment operator is =, but you still do not want to change the variable props as I stated above.

The line above would only push a string “tracks” into an array IF collection[i][prop] is already an array.

The above would only push value into collection[id][“tracks”] IF collection[id][“tracks”] is already an array. If there is not already an array present, you can not use the push method without getting an error.

The easiest way to know if a property already exists in an object is to use the hasOwnProperty method. For example, if I wanted to check if the current objected being iterated over in the for loop has a “tracks” property, I would write:

if (contacts[i].hasOwnProperty("tracks") {

If I wanted to check if an object had a property contained in a variable named prop, then I would write:

if (contacts[i].hasOwnProperty(prop)) {

One last hint for you: If you are able to determine if the current object has a “tracks” property (meaning it contains an array already), you could push the value into this array in one of three different ways:

contacts[i].tracks.push(value);

OR

contacts[i]["tracks"].push(value);

OR since you would already know that prop is equal to tracks at this point, you could write:

contacts[i][prop].push(value);

But what if you need to add a value (a song) to the object’s “tracks” property, but no such property exists. This means there is not already an array to hold the song names? Then, you could simply assign a blank array to a “track” property, which will do two things. First, it will create a “tracks” property. Second, it will assign it an empty array in which you could then push a value to (I showed you 3 ways of doing that above).

You should now have enough information on how to assign a value to a property. In this case it would just be an empty array. If you can not remember how to create object properties and assign them values, I suggest you review the previous FCC challenge.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/updating-object-properties/

You still have many other syntax and logic issues you need to figure out, but that is why I asked you to write out your algorithm first, because you do not seem to understand the logic need in the algorithm. Because you do not understand the correct algorithm, you will not be able to write the correct code.


#15

Okay. I figured it out, but I’d just like an explanation of exactly what I did because I was just messing around for a bit because my brain was starting to go numb.

This as where I seemed to answer correctly but I’m not exactly sure if what I did was proper coding or simply a loophole I found to get a thumbs-up from the system.

  else if(!collection[id].hasOwnProperty("tracks")) {
    collection[id]["tracks"] = [];
    collection[id]["tracks"].push(value);
  }

  else if(value !== "") {
    collection[id][prop] === [];
    collection[id][prop].push(value);

Previously, both of these if statement had the collection[id] with triple equal signs to the empty array. However, I changed the top one to an assignment symbol and the code worked. Does this have something to do with the .hasOwnProperty() method I used or is it something else.

By the way, thanks for your patience with me.


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== ""){
    collection[id][prop] = value;
  }
  
  else if(!collection[id].hasOwnProperty("tracks")) {
    collection[id]["tracks"] = [];
    collection[id]["tracks"].push(value);
  }

  else if(value !== "") {
    collection[id][prop] === [];
    collection[id][prop].push(value);
  }

  else if(value === "") {
    delete collection[id][prop];
  }
 
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


#16

That was never doing anything. It was just a logical comparison, but if it is not in an if of if else statement it has no functionality.

The assignment operator is = . It is typically used to assign a value to a variable.
The === operator is a logical comparison operator which is asking is if what ever is on the left side of it strictly equal to what is on the right side of it AND that both are of the same data type.

There is also the == operator which just called the equality operator which just tests that both sides of it are equal.

Here is some code to illustrate the differences between all three of these.

// Assignment operator
var a = 3;  // assigns the number 3 to variable a
var b = "three";  // assigns the string "three" to variable b
var c = "3"; // assigns the string "3" to variable c
var d = 3; // assigns the number 3 to variable d

// Strict Equal operator
a === b // evaluates to false => 3 is not strictly equal to "three"
a === c // evaluates to false => 3 is not strictly equal to "3" - a is a number and b is a string
a === d // evaluates to true => 3 is strictly equal to 3 - both are the same data type

// Equal operator
a == b // evaluates to false => 3 is not equal to "three"
a == c // evaluates to true => 3 is equal to "3"
a === d // evaluates to true => 3 is  equal to 3