Why can't I seem to get any better at coding at all

Tell us what’s happening:

I keep having problems with task like this. Whatever I try and improve myself to learn doesn’t help.
Recaping lessons doesn’t seem to help much.
Trying to code my own projects also end up in failure.
Watching alternative YouTube video’s and following allong the tutorials doesn’t help either. I tried so much to get better but, my code end up still like the one bellow utterly thrash. Why can’t i get better at it?

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) {

if (prop != tracks && value != ""){
return update set value;
}else (prop ==  album != tracks)
return empty array and add new value to album corresponding prop;

else prop == "tracks" && value != "" 
return.push() value end of the album exsisting tracks array
else if value == ""
return delete prop from album


return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

First of all, here is a quote I love from the book “Eloquent Javascript”:

Because computers are dumb, pedantic beasts, programming is fundamentally tedious and frustrating. - Marijn Haverbeke

Could you solve this exercise previously? I accidentely have stats on how hard the FCC exercises were for me. (from Anki app) And the record collection was the one that I had to repeat most often …

2 Likes

To be honest no not really. Not on my own. This is one of the lessons I keep having trouble with each time I try it.

Here is a walkthrough (in case you don’t know it yet)
Record collection at: 2:03:45

2 Likes

Looking at the code you post, you seem to rush and just try things without thinking about how the solution should work first.

I recommend starting a coding challenge by not even touching your computer. You want to grab a pencil and paper and outline how the solution should work. Move carefully and plan what you will do. Ask yourself why you are adding every part of the code. Slow is fast!

2 Likes

Maybe im missing something, but does that code fullfil the following rule?:

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

I mean, the property is created but not with the name “tracks”.

If your struggling with something, understand that that is to be expected. If your not “getting better” then keep grinding against it. Heck try to do that challenge from scratch 100 times. Odds are you will fail ** a lot**, have problems, but every time you go thru it again you will learn something new. I’m pretty sure it wont take 100 times, but each iteration you end up needing help, finding answers, and fixing your code is another lesson in itself.

The lessons, youtube videos and tutorials are there to give you a taste of what you learn. Working on your own stuff is the real lesson. You could be failing them for different reasons. Your personal projects could fail due to not understanding the tooling, the problem, or your own abilities.

Better doesn’t mean instant success, better means you learn something from your failure. You don’t learn anything but continuously succeeding non-stop.

4 Likes

I ran through the course yesterday and today, and this was one of my least favorite of the assignments because the requirements are a little unclear, and the function you are being asked to write does too many things that should be separated out into smaller pieces.

Here’s what I ended up with, and the main reason I have this code is that I failed tests initially and I wanted to debug it, so I put it into a standalone html file I could edit in atom + liveserver and debug in chrome easily. As someone who already knows javascript fairly well, it was one of only a few lessons where I wrote code that didn’t pass on the first attempt.

In particular, here is where there is some possible confusion to the way the requirements are described:

If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property.

Then …

If value is empty ( "" ), delete the given prop property from the album.

In the first case you have a requirement that is also telling you how to do something, which is unusual. There is no reason that I should have to create an empty array prior to adding an array property, so long as it’s clear that the requirement is that tracks is an array of strings, with the titles of the tracks – should it exist.

In the case where value is empty, how do you interpret the “tracks” instruction that precedes it? Typically ordering of requirements is important. Things fell into place for me once, I concluded that the first decision point, is whether or not the value param is empty.

Case #1:  (value param is empty) 
  --  Remove the prop if it exists.

Case #2:  (value param is  not empty) 
     -- Update the object property
    2a.  (prop is "tracks"):
         2ai. ("tracks" prop doesn't exist?): 
            -- create it as empty array
         -- push value onto "tracks" array.
   2b.  (prop is not tracks):
   -- update property

The most important decision I had to make was how to prioritize and order the decisions.

The 2nd decision was deciding whether or not the prop was “tracks”.

Once it was clear that these decisions matched the requirement, writing the code had a clear and simple structure with an if - else.

There are some things about javascript that make working with javascript objects, such that they are, very easy. For example, unlike most languages, you can just add a property to a javascript object anytime you want using either the dot or array notation. If the property existed, it updates it, and if it did not, it adds it as a property.

The other thing I think this lesson is trying to test you on, is your understanding of when you can call a built in class function like array.push(). If you try and call that code on a property that is not an array, you are going to get a runtime error, so this lesson is emphasizing that point, and trying to make sure you think that through and make sure that you have an array to work with prior to trying to call its push function.

One way to get better at programming is to read other people’s code, break it down and be sure you understand how it works.

Now by no means do I think what I ended up with is great code, but it is a solution that passes the tests, and follows the outline above. I didn’t write down that outline, before I wrote the code, but in terms of writing some pseudocode, it would have allowed me to write the code I eventually came up with.

If you want to see the code I ended with, here it is:

function updateRecords(id, prop, value) {
  if (value != "") {
    if (prop == "tracks") {
      if (!collection[id].hasOwnProperty(prop)) {
        collection[id][prop] = [];
      }
      collection[id][prop].push(value);
    } else {
      collection[id][prop] = value;
    }
  } else if (value == "" && collection[id].hasOwnProperty(prop)) {
    delete collection[id][prop];
  }
  return collection;
}

Personally, what I try to do is short circuit functions whenever possible. What I mean by that is that if you have a condition where the requirement allows you to decide a function result, you should move that to the top of the function, rather than have an if-then construct. For example, in this exercise, once I concluded that there was a branch on value being empty, I could have written this code:

  if (value == "") { 
    if (collection[id].hasOwnProperty(prop)) {
      delete collection[id][prop];
    }
    return collection;
  }

// execution only continues if there is a value

This is best practice, and would have been better than what I wrote, and if it was important enough to review it, I would probably have seen that and gone back and refactored it.

Learning to code is a journey that never ends. You will get better at it the more you practice, the more you read other code, and the more you work on projects with other people, many who will be better coders than you are.

Try to remind yourself that like most things, you will get better at it if you are persistent. I think that a lot of people think that you “learn how to program” in a particular language, and after that you write perfect code and if you aren’t/don’t then you’re never going to be able to program, when that just isn’t reality.

Secondly, javascript is a strange language with some unusual features that can be very confusing. It’s also been evolving rapidly the last few years. On top of that, the reason people are often learning or using javascript is because they are writing clientside UI code for browsers, which introduces a whole slew of complicated technologies. Try not to let it overwhelm you.

Last but not least, this course is less a course on javascript than it is a quick (albeit complete) survey of the language basics. There are books and courses that take a lot more time illustrating and going into examples. The important syntax you want to get out of this course most of which were borrowed from the c language:

-variable types and assignments
-operators including postfix (++,–) and shortcuts
-the ternary operator
-what is a string and what built in properties are useful with strings
-what is an array and what are some common array functions
-global vs local scope
-types of loops (for, while)
-if then else and switch
-functions, parameters and return values
-javascript object syntax (and json)

This is just off the top of my head, but once you have a good idea about how these work in javascript, you will find the concepts not to mention the syntax is nearly the same in many other languages including c, c++, php, objective c and c# to name just a few.

2 Likes

Are you refering to the following bit in the video?
image

I think it’s fine, since the new property’s name is the value of prop and that value is "tracks". Therefore it’s called “tracks”.

https://jsfiddle.net/h98r3w6u/

1 Like

Yeah. The syntax was a bit confusing to me, but i tried it out myself, breaking the function down into smaller pieces to understand.

Is comparing against undefined something you would recommend?

if(collection[id][prop]){     //IF THE PROP EXISTS
  collection[id][prop].push(value);     //PUSH THE VALUE
  }
  else{     //IF NOT
    collection[id][prop]=[value];     //CREATE AN ARRAY WITH THE PROP NAME THAT CONTAINS THE VALUE
  }

This is from the official guide (with my own comments), it seems cleaner and easier to understand.

Thanks!

1 Like

Best coding counsel ever!!!

1 Like

Wouldn’t have come to my mind… Last time I revisited this exercise, I wrote it like this:
if (collection[id].hasOwnProperty("tracks") === false) {

Not clever … yet my mind can process it intuitively … I don’t think I am at a level where I should comment on what is the “best” solution here. Can only speak for myself.

1 Like

Yes i agree. :cat: this is one of the best things ever. Not only did I learned that I should use paper and take it slow and allot of other tips and tricks but, that I was not the only one that was having this problem and was strugeling so much onto it. Thank you and everyone for the great advice <3

1 Like

Have you reconsidered your first language to jump off with? Maybe come back to it?

Something I did to try because I’m like a failure expert sometimes, is to dig into the weeds at a fundamental darn near 1s and 0s to understand it at its root. Then try to come out and try again after that.

I chose Java first with a plan to select another 1 or 2 to develop a firm understanding of depending on how much I was out there flapping with my choice. I am currently scrambling out of the weeds after literally going back and reviewing some information on how the computer interprets and communicates what we give it.

Err…don’t know if that helped.

1 Like

I have been working as programmer for about 4 years and at least every few months or so I feel like I haven’t learned anything and I’m not improving. But then when I look back to how my skills were two years ago, or even last year, I have improved sooo much. Programming is hard, you should be gentle with yourself. You don’t always recognise your own progress.

Doing tutorials online or working on my own personal projects have never really been my thing. I can’t focus on my own projects, I find them tedious. Online tutorials where you have to write an algorithm always annoyed me so much. But you learn doing them by time.

When you’re reading the assignments or codes, try to change the code “language” to your own. What is the code doing? do that line by line and take your time. You are not in hurry when you’re these assignments. It also helps to draw it or write them down. Anyways that helps you make sense of it. And remember, you can always use google, to find ways to solve your code.

I know this is dumb and everyone says that, but try to learn the basics concepts of language. How does the the code work there? What are values, what are object, how do they interact with each other? I recommend signing up for Dan Abramov’s “Just Javascript” email chain, if you’re trying to learn Javascript. https://justjavascript.com/

Hope you do you notice your improvements one day!

1 Like