Basic JavaScript - Record Collection

Hi all, Having some probs getting this to work, UGH!
I know there might be more than one solution to solving this and there is a ‘preferred’ solution as well.

  1. Any advice on how to more efficiently look at the directions and decide what steps should be solved first and which last. The order of things. I’ve been over this several times diff ways, but seem to keep getting some logic wrong with the approach.
    Maybe it would help enlighten others (with no strong programming background) to know how to look at the logic approach and understand how to tell what steps in the word problem should be solved first, etc.

  2. Here is my last approach. Could anyone help me understand why this wouldn’t be working as well.
    I seem to keep getting stuck and can’t figure out if it is because i’m doing the steps in the wrong order or if it is just a programming mistake.

Thanks in advance,
FYI, I try not to spend too much time trying to solve things. I don’t like wasting days on something I can’t figure out.

// 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"
  }
};
---------------------------------------------------------
// Only change code below this line
function updateRecords(id, prop, value) {
  // If value is ""
  if (value === "") {
    delete collection[id][prop];

    // If prop is NOT 'tracks' && value is NOT "", than set the value
  } else if (prop != "tracks" && value != "") {
      collection[id][prop] = [value];

      // If prop IS 'tracks' && .hasOwnProperty('tracks') IS false
  } else if (prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
      collection[id] = [prop];

      // If prop IS tracks && value is NOT ""  
  } else if (prop === "tracks" && value != "") {
      collection.id.prop.push(value);
  }
  return collection;
}

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

Programming is all about solving things (regardless of the time spent to do it). Be prepared for many days/weeks of not being able to figure things out in the beginning. This is just part of the natural process of learning how to program and developing problem solving skills. It takes time and practice, but at some point, things will get easier.

Before writing any code, make sure that you understand what steps you would need to take in order to solve the problem without a computer. This could be as simple as writing down the steps you need to perform to obtain the desired result of each test case. When you can write these steps out in plain language and walk through the test cases using the steps and come to the expected outcome of each test, you have a working algorithm which is 90% of the work. The remaining 10% is to translate the algorithm steps into code using the basic JavaScript syntax you have learned in previous challenges.

It is very tempting to just start writing code and hope you can get something working, but without a specific outline of what needs to be done, you can end up wasting hours/days/weeks with many rewrites.

The Record Collection challenge is one of the first “difficult” challenges you encounter where you really must truly understand the requirements of the instructions and map out an algorithm before writing the code. This is even more important if you have no strong programming background.. The bottom line is it takes lots of practice solving various problems until you start to see patterns in the way problems are stated and the solutions that would be required for them.

OK, let’s talk about the code you posted.

The first case you are failing is below. You are failing it because you should be adding an “artist” property with a value of “ABBA” to the 5439 object. Your solution does add an “artist” property to the 5439 object, but you add the value ["ABBA"], because your first else if statement assigns [value] instead of just value.

updateRecords(5439, "artist", "ABBA")

The next test case you are failing is below. You are failing it because you should be adding the property “tracks” to object 5439 an assigning it the value ["Take a Change on Me"]. There are two separate issues with your code. First, in the second else if , you are trying to assign an array with the value of prop (“tracks” which is a string). Also, should be assigning it to the “tracks” property of object 5439, but instead are trying to add replacing the object with the array. You need to specify the property name “tracks” on the collection[id].

updateRecords(5439, "tracks", "Take a Chance on Me")

Once you fix the problems of the first two test cases mentioned above, you will pass all but one test case (below).

updateRecords(2468, "tracks", "Free")

I am going to let you see if you can figure this one out on your own. If you fix the other two and get stuck on the last one, please repost your new code attempt and we will guide you a bit more.

Happy coding!

3 Likes

Hi there, Just a quick update.
I was able to figure out a couple of my problems.
I’m still tinkering with the last one. Seems like my order of things might be off, or the logic is not exactly right still. I’ll keep looking at it today and get back to you with more.
THanks

Ok, I figured it out. Had to redo my logic a bit, but it works.
Had to do some reading up on some things, but did not look at answers.
For the life of me, I could not figure out why I could not get ‘.hasOwnProperty()’ method to work on the ‘collection’ object. So I had to try to improvise somehow.
I’d still like to know if I was supposed to use ‘.hasOwnProperty’ or not, to get it to work.

Here’s the code:

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

**Note: Part of the learning problem is that when you’re trying to look elsewhere for info on how things work, you can’t be sure of the age of the references you’re looking at, since lots of them have no post dates. So you don’t know if you are reading out dated info or not.

This could get you in trouble if the value of the object property was 0. Why? Because 0 is a falsy value in JavaScript. It is better to use hasOwnProperty like:

      if (collection[id].hasOwnProperty(prop)) {

are you sure you were using it on the right object? is prop a property of collection?
what are the properties of collection?

OK, so I’m finally able to get the ‘.hasOwnProperty’ to work correctly now.
After isolating and playing with this part of the problem (the “tracks” area).
All I need is more practice now.
BTW, anyone have any pointers of where to go to look for more exercises for these exercises. Would be nice if you get some repetition with more exercises somehow.
I’ll keep coming back to this to practice as well.
Thanks for all the input.
Here’s my final code:

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

there is the interview prep section that has a lot of algorithms and also you can go to something like CodrWars website, which is just algorithms

I would suggest you still keep going through the Certification as there are more algorithms to come.