[Solved] Basic JavaScript: Record Collection. Might be misunderstanding question

Tell us what’s happening:
I think I’ve written a good solution, but the test module disagrees. Looking at its feedback, I might be misinterpreting it. The issues I’m currently working to fix are theese:
After updateRecords(5439, "tracks", "Take a Chance on Me") , tracks should have "Take a Chance on Me" as the last element.
After updateRecords(2548, "artist", "") , artist should not be set

I’ve traced my code multiple times, and I don’t see how my solution doesn’t satisfy those requirements. I feel I may have misunderstood something despite the instructions, so yeah.

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

// Only change code below this line
function updateRecords(id, prop, value) {

var album = collection[id];

if (shouldDeleteProp(prop, value))
{
  deleteProp(prop);
  return;
}

handleUpdatingTracks(album, prop, value);

handleUpdatingOtherProps(album, prop, value);

return collection;
}

function shouldDeleteProp(prop, value)
{
return prop != 'tracks' && value == '';
}

function deleteProp(album, prop)
{
delete album[prop];
}

function handleUpdatingTracks(album, prop, value)
{
if (prop !== 'tracks')
  return;

if (valueIsEmpty(value))
  deleteTracksFrom(album);
else
{
  addTrackTo(album, value);
}

}

function valueIsEmpty(value)
{
return value == '';
}

function deleteTracksFrom(album)
{
delete album.tracks;
}

function addTrackTo(album, track)
{
if (!albumHasTracks(album))
{
  album.tracks = [];
}

album.tracks.push(track);
}

function albumHasTracks(album)
{
return album.hasOwnProp('tracks') && album.tracks.length > 0;
}

function handleUpdatingOtherProps(album, prop, value)
{
// No need to check if the value is empty, since we already
// have that accounted for elsewhere
album[prop] = value;
}

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

Your browser information:
Google Chrome (Brave) 80.0.3987.122
Windows 10
WebKit (537.36).

Challenge: Record Collection

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read. Please note: Using the Ask for Help button prepopulates a forum post with you current code; there is no need to paste your code again.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

I am using your code, and running the function calls you have issues with. One is erroring out and not running, the other has the wrong output.

updateRecords(5439, "tracks", "Take a Chance on Me")
// output:
// TypeError: album.hasOwnProp is not a function

Here there is something wrong in your code

updateRecords(2548, "artist", "")
// output:
// undefined

Always check what your function is returning

@Sky020: Understood, thank you.
@ilenia : How do I check what my function returns when there’s no debugger for me to use? And console.log messages don’t even show up in the test output window.

you can use browser console or a different editor (I like repl.it)

And if you wrap the function call in a console.log statement it will even appear in the freecodecamp console (which has its limits but can be useful enough)

Understood. I decided to use VS Code and figured what I did wrong through some debugging :slight_smile:

  • I misspelled the hasOwnProperty method
  • I let handleOtherProps execute when the prop to change was the tracks
  • I passed the wrong arguments to the deleteProp method

I passed the challenge after submitting my new code. Thanks for the help :slight_smile:

2 Likes