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");

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.

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.