Record Collection Challenge

Record Collection Challenge
0.0 0

#1

I’m on the record collection challenge, but I’m having trouble with it. This is the function I coded, which doesn’t seem to be doing anything to the record collection:

function updateRecords(id, prop, value) {

if (id === true && prop === false) {
collection[id] = new Object(prop);
collection[id][prop] = value;
} else if (prop === true && value !== “”) {
collection[id][prop].push(value);
} else if (prop === true && value === “”) {
delete collection.id.prop;
} else {
return console.log(“invalid”);
}

return collection;

}


Here’s the instructions:

Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection. If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property. If value is empty (""), delete the given prop property from the album.

Can someone point out what I’m doing wrong?

Thanks!


#2
  1. prop isn’t boolean (true or false) its a string
  2. Why are you creating an object?

#3

If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

if (id === true && prop === false) {

id is never 1 so this if never executes.

else if (prop === true && value !== "")

prop is never 1 so this and the next else if never executes (if first statement in && is false second statement gets ignored).

So your function always just prints ‘invalid’ to console.

EDIT:

Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.

Misread === for == in your code :blush:
Your ifs will never evaluate to true even if id or prop will be 1. So no matter what your code always print ‘invalid’ to console.


#4

Whoops…I did not realize that that was what I was doing. I was just trying to get the function to check whether the prop the user typed in (when using the function) existed or not. Do you know if there’s any way to do that?


#5

1 & 2: I think I just got so confused…

But thanks for pointing out both. As for number 2, I thought I needed to create an object that nested itself within id, but now that you pointed it out, that’s not what the instructions call for…


#6

@Shmi1

One of my practices to help understand instructions is to put each instruction on a separate line. Then, indent related instructions. Then, capitalize any action words.

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.
 
IF prop IS "tracks" AND value ISN'T empty (""),
  PUSH the value onto the end of the album's existing tracks array.

IF value IS empty (""),
  DELETE the given prop property from the album.

Challenge provided hints

Use bracket notation when accessing object properties with variables.

Push is an array method you can read about on Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push.

You may refer back to Manipulating Complex Objects Introducing JavaScript Object Notation (JSON) for a refresher.


#8

@plkjln - What is the purpose of your reply to a topic that is over 2 years old?