Confused with JS freecodecamp tutorial
Requesting Guidance with Record Collection problem in JavaScript Algorithms and Data Structures curriculum
I used the switch statement it made things easy for me
More on reddit.comBasic Javascript: Record Collection [Please Help]
You're really close! It looks like each of your branches has a minor mistake or 2.
You want to be using
=for assignment, not==which is for comparison.Right now you'll never hit this branch, because you're checking for the
tracksproperty on the whole collection, you want to check it for a specific id.
You're also catching one test you don't want to be in this branch. updateRecords(collection, 2548, "tracks", "") will go to this branch, because it has the property tracks. You also need to check that it doesn't have a value.
Once you're in there, you're really close! You just want to push only the new track (aka value) not an array of the value. push will put whatever you give it into the array, it doesn't squish 2 arrays into 1 (concat does that)
3) You'll see when you're hitting this branches it says TypeError: Cannot read property 'push' of undefined. This makes sense because object[id][prop] which in the first test parses to collection[5439]tracks doesn't exist, and you can't push to an array that doesn't exist. You just want to assign it to your value instead.
4) You're just missing the return statement here :)
Without further ado, this is how I would modify your code to pass all the tests:
function updateRecords(object, id, prop, value) {
if (prop !== "tracks" && value !== "") {
object[id][prop] = value;
return object;
} else if (prop == "tracks" && object[id].hasOwnProperty("tracks") && value) {
object[id][prop].push(value);
return object;
} else if (prop == "tracks" && value !== "") {
object[id][prop] = [value];
return object;
} else if (value == "") {
delete object[id][prop];
return object
}
}
Finally here is how I would refactor / reorganize your code to make it a little cleaner. This step is totally optional though!
function updateRecords(object, id, prop, value) {
if (!value) {
delete object[id][prop];
} else if (prop !== "tracks") {
object[id][prop] = value;
} else if (object[id].hasOwnProperty("tracks")) {
object[id][prop].push(value);
} else {
object[id][prop] = [value];
}
return object
}
More on reddit.com Javascript basic algorithm scripting - How I can solve these problems? Help! : FreeCodeCamp
Videos
So I just recently finished the html and css sections of freecodecamp and went to JS next on my journey to hopefully become a front-end developer somedays. After a couple of hours of JS tutorials I am having a really hard time adjusting to this new style of learning.
With html and CSS I could pretty much immediately see results when I learned a new concept because it was visualized right away. With Javascript, I am learning about Arrays, Strings, functions etc. but I have zero imagination about what to do with any of these. I am just wondering if this will become better the longer I keep studying or if there is a better, sort of hands-on way to learn with real-world examples and use cases of different concepts I am learning.
Happy about any input!