Record collection challange

Record collection challange
0

#62

Glad to see your answer since this is the only one going the same way with mine except for one point,the order inside the first else if.

function updateRecords(id, prop, value) {
if (value === “”){
delete collection[id][prop];
}else if (prop === “tracks”){
if (collection[id].hasOwnProperty(“tracks”)){
collection[id][prop].push(value);
}
collection[id][prop] = [];
collection[id][prop].push(value);
}else{
collection[id][prop] = value;
}
return collection;
}

And when I ran the code above calling the function as below,

updateRecords(2468, “tracks”, “Free”);

the array of “tracks” property inside the “2468” turned to be a single “Free” instead of a result of pushing.
But as soon as I changed the order the same as yours things worked out smoothly and I wondered the reason underneath. Are there any logical problems?


#63

Hello felps85,
I found an error in your code. You don’t need to push the value inside the function the one outside the function does it for you. The way you have it coded will push the value twice. Here is an update to your code that works:

function updateRecords(id, prop, value) {
if(prop !== “tracks” && value !== “”) {
collection[id][prop] = value;

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

} else {
delete collection[id][prop];
}

return collection;
}


#64

Hi, i don’t really have much to add to this topic, but i would like to say that this challenge is way too difficult for people that is just starting to code, don’t get me wrong the challenge is good and it will really make you think, i spent hours trying to figure it out but i finally had to search for hints on the forum to get it working correctly, the challenge includes some aspects that weren’t still covered win the course, a nice suggestion would be to present challenges like this latter in the map, i’m sure just like me a lot more people felt probably demotivated by not being able to figure out the code, other than that i’m very pleased with FCC.

.


#65

Hi telleddy,

Yeah, your code will work if you add an else statement after the 2nd if statement.
So it should be,

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

etc…

You need this else statement because it should only create a new tracks array if it is not already present. Otherwise, it probably will erase the original data or something like that.


#67

get it and thank u sooooo much!:kissing_closed_eyes:


#68

the wording for this challenge through me off. the checks on the bottom arent easy to follow when compared to other lessons.


#69

This is what I did, but it’s not concise.

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

return collection;
}


#70

This might help.
I started with a pseudo code meaning use plain English to get the logic. Then translate the English into code.
Here we go.
if prop !== tracks && value !== “” then set the value as a property of the given id.
For example: update(5439, “artist”, “ABBA”) ===> since prop isnt tracks, artist gets set as a prop in 5439 and artists takes value ABBA.
lets continue
if prop === tracks && album has no tracks then we are going to create an empty array for tracks and pass in value in the array
here we check if album has tracks and if it doesn’t then we create empty array but if the album has a track prop then we want to add value at the end of the existing lists in the track array.
if prop === tracks && value !== “” then we’re going to push value into the last position of the existing array.
if value ==="", then delete the prop from album.

function updateRecords(id, prop, value) {

if (prop !== “tracks” && value !== ‘’){
collection[id][prop]= value;

}else if (prop === “tracks” && value !== ‘’){
if (collection[id].hasOwnProperty(“tracks”) === true){ // if tracks prop exists then we just push value into track arraindent preformatted text by 4 spacesy
collection[id][prop].push(value);
}else{collection[id][prop]=[value]; // however, if it doesn’t have track we create it and push value into it.
}

}else {
delete collection[id][prop];
}

return collection;
}

Hope it helps!!!


#71

Jenovs had a good breakdown. Similar to what we probably should have been able to figure out with the instructions from the lesson.

Like many others, this lesson stumped me. I had to sit here reading forums to truly grasp it. There seems to be many ways to simplify the code, but I wrote it in a way that made the most sense given the instructions. Looking around for other answers I came across -

function update(id, prop, value) {
if (value === ‘’) {
delete collection[id][prop];
} else if (prop !== ‘tracks’) {
collection[id][prop] = value;
} else {
collection[id][prop].push(value);
}

return collection;
}

I can’t find myself coming up with this code with the instructions given. I’m just not there yet. So following along with the information given and commenting it out and then figuring it out from there gave me an easier understanding.

// Only change code below this line

function updateRecords(id, prop, value) {

//If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.

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

//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.

  • This was probably the hardest part for me to really figure out on my own.

if(prop === “tracks” && collection[id].hasOwnProperty(“tracks”) === false){
collection[id].tracks = [];
}

//If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

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

//If value is empty (""), delete the given prop property from the album.

if(value === “”){
delete collection[id][prop];
}

return collection;

}


#72

I struggled with this one too. Took me way longer than I thought it should. Here is my solution.

I think I worked it backwards, removing prop first and then updating the tracks, then working on adding new ID items. This seemed easiest to me, and figured it might help someone else.

function updateRecords(id, prop, value) {
  // Remove the prop's to delete
  if (value === "") {
    delete collection[id][prop];
  }
  // Then check if prop is tracks, and either push or create array and push
  else if (prop === "tracks") {
    if (collection[id].hasOwnProperty("tracks")) {
      collection[id][prop].push(value);
    }
    else {
    collection[id][prop] = [];
    collection[id][prop].push(value);
    }
  }
  // Finally, add new values
  else {
    collection[id][prop] = value;
  }
  
  return collection;
}

#73

muchris - Thank you for the comments in your code. I felt that the instructions were not very clear on this challenge, but looking at your code with the comments really made it clear. Thanks again!


#74

Just found this today, thanks so much breaking it down! It really helped!


#75

Ok, I think It’s a real mess from my side, somehow I managed to pass the challenge with the following code, but it just doesn’t seem like a right way of doing it. I am so confused at this point, especially because I passed the challenge but my code is completely different from anyone else above…

function updateRecords(id, prop, value) {
collection[1245].tracks = [“Addicted to Love”];
collection[5439].artist = “ABBA”;
collection[5439].tracks = [“Take a Chance on Me”];
delete collection[2548].artist;
delete collection[2548].tracks;

return collection;
}

Did I just hacked into a passing a challenge like this, and my code doesn’t make any sense at all?


#76

@NikolaP Yeah, you just hard-coded the solution there.

Here is the link to an official solution with the explanation:

Solution:

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

Code Explanation:

  • First checks if prop is equal to tracks AND if value isn’t a blank string. If both tests pass, value is pushed into the tracks array.
  • If that first check doesn’t pass, it next checks only if value isn’t a blank string. If that test passes, either a new key (prop) and value (value) are added to the object, or an existing key is updated if the prop already exists.
  • If both these checks fail (meaning value must be an empty string), then the key (prop) is removed from the object.

Example Run

  • updateRecords(5439, "artist", "ABBA"); runs.
  • prop is equal to “artist”, not “tracks”, so the first part of the else if statement fails.
  • value is not a blank string, so the second part of the else if statement passes.
  • artist: "ABBA" is added to the 5439 id.

Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

Thanks @leftynaut @DarrenJansen for your help with Checkpoint: Record Collection

p.s: I also found this tutorial which may prove to be helpful in understanding the solution: https://www.youtube.com/watch?v=ZmcKUL1Gm98


#77

I see, thanks for your reply

&Thanks @leftynaut @DarrenJansen for your help with Checkpoint: Record Collection


#78

It took me time to achieve this. It’s pretty simple code if you know exactly what the exercice want you to do:

function updateRecords(id, prop, value) {
	//convert to string for easiest coding
	var numId = id.toString();
	
	//if it's not new
	if( collection.hasOwnProperty( numId ) ) {
		if( prop == "artist" ) {
			if( value == "" ) {
				delete collection[numId][prop];
			
			}else {
				collection[numId][prop] = value;
			}
		
		}else if( prop == "tracks" ) {
			if(  value == "" ) {
				delete collection[numId][prop];
				
			}else {
				//array already exist so only .push()
				if(collection[numId].hasOwnProperty( prop ) ) {
					collection[numId][prop].push( value );
				
				}else {
					//array do not exist yet so create it then push()
					collection[numId][prop] = [];
					collection[numId][prop].push( value );
				}
			}
		
		}else if( prop == "album" ) {
			if( value == "" ) {
				delete collection[numId][prop];
			
			}else {
				collection[numId][prop] = value;
			}
		}
	  
	}else {
		//if it's new, create it then
		collection.push( numId );
		
		//the code is same
		if( prop == "artist" ) {
			if( value == "" ) {
				delete collection[numId][prop];
			
			}else {
				collection[numId][prop] = value;
			}
		
		}else if( prop == "tracks" ) {
			if( value == "" ) {
				delete collection[numId][prop];
				
			}else {
				if(collection.hasOwnProperty( prop ) ) {
					collection[numId][prop].push( value );
				
				}else {
					collection[numId][prop] = [];
					collection[numId][prop].push( value );
				}
			}

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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log( collection["5439"]["artist"] ); //"ABBA"

updateRecords(5439, "tracks", "Take a Chance on Me");
console.log( collection["5439"]["tracks"] ); //"Take a Chance on Me"

updateRecords(2548, "artist", "");
console.log( collection["2548"]["artist"] ); //"undefined"

updateRecords(1245, "tracks", "Addicted to Love");
console.log( collection["1245"]["tracks"] ); //"Addicted to Love"

updateRecords(2468, "tracks", "Free");
console.log( collection["2468"]["tracks"] ); //"1999"

updateRecords(2548, "tracks", "");
console.log( collection["2548"]["tracks"] ); //"undefined"

updateRecords(1245, "album", "Riptide");
console.log( collection["1245"]["album"] ); //"Riptide"

#79

Holy shit it worked! Thank you for explaining this. Had the same issue and the “bug,” kept flipping the answer. After removing it, it passed.