Record Collection Push() Confusion

Tell us what’s happening:

First of all, this is my first forum post and I’d like to say it would be hard to overstate the benefits of this project. Thank you to the administrators, so much.

For this challenge, I found a solution of my own but I was looking in the hint section to see how it was done, and I am not sure if the solution given is somewhat incomplete or if I am misunderstanding it.

In the given solution, if the property is “tracks”, and the album in question does not have a “tracks” property, the line of code executed is: “collection[id][prop] = value”.
But then if you were to send another value to this album’s tracks, it looks like it would try to push() it on, but I think it would be pushing a string onto a string, not onto an array. So I tried to see if you can turn a string into an array in JS by simply pushing another string to it, but it didn’t seem to work that way. Does it?

But maybe the solution in the hint section is only required to work for a single iteration of the function. That would make sense here.

Clarification would be much appreciated.

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 (value == "") {
    delete collection[id][prop];
    return collection;
  }
  switch (prop) {
    case "tracks":
      if (collection[id].tracks == undefined) {
        collection[id].tracks = [""];
      }
      collection[id].tracks.push(value);
      break;
    case "album":
      collection[id].album = value;
      break;
    case "artist":
      collection[id].artist = value;
      break;
    default:
      break;
  }
  return collection;
}

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


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

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

Remember that an array with an empty string is not the same as an empty array.
[""] is not the same as [].
Whe we push 'ArielLeslie is Awesome' to each we get
["", "ArielLeslie is Awesome"] and ["ArielLeslie is Awesome"]

Oh good point. I should change that in my answer. It accepted my answer, anyway, though.

EDIT: Well, these answers don’t persist, so. Good to remember, though. Thank you.

I’ve re-read your question a couple times and I’m not entirely sure what you’re asking.
The intention of this challenge is that tracks will always be an array even if it contains only one string.

In terms of converting strings to arrays, you can use a split if you expect the string to generate multiple array elements or if you want it to be the first element in an array, you can either push it or just initialize the array with that string in it.

I think you may be thinking of this line:
collection[id][prop]=[value];
Which is different from the one you wrote. Note the brackets around value, they initialize an array.

1 Like

That’s what I wasn’t seeing! Thank you! With all the brackets around properties for bracket notation, I didn’t even notice it.

Initialize the array with the string in it. That’s what I was missing.Thanks!