Struggling with Javascript

Struggling with Javascript
0

#1

This is my third website trying to understand javascript. I can not seem to grasp the concepts of it. Every time I get to a certain point my brain fries and I can’t make sense of the language. With FCC I made it all the way through the challenges with little to no problem until I hit the Record Collection challenge. This one completely stumped me. I am working my way back through the challenges to hopefully understand it better but I always seem to have an issue understanding javascript. Would it be beneficial to read a book on it or should I just keep trial and error until I work my way through my javascript woes? I am not going to give up I would just appreciate some advice on how to get a better understanding with the language since it continues to frustrate me to no end.


#2

Record Collection (and Profile Lookup) stumps lots of people. You could search this forum for previous discussions and that may provide enough explanation to get you unstuck.

If you haven’t come across it yet, the series You Don’t Know JS is something you will want to read eventually. You can read it for free on GitHub. This chapter relates to you current problems:

Other than that, just take your time and seek out as much material or advice as you need until you get it.


#3

If you’re a visual learner, I’d highly recommend Jon Duckett’s JavaScript & jQuery book. It was an excellent supplement for me as far as visualizing the way things flow and interact with each other.


#4

You made my day with that link. Nice find!


#5

Warning: long answer ahead.

Handing combinations of arrays and objects is HARD when you’re first learning it. I run a coding group with over 400 members, and this one concept is where I see the greatest amount of attrition in my group. So you’re not alone in your struggles.

When I was first getting started I just tried to remember that arrays are just arrays, and objects are just objects.

You probably know how to access or change and array element:

var myArr = [1,2,3,"apples"];
console.log(myArr[0]); // logs 1;
console.log(myArr[3]); // logs "apples
myArr[3] = "pears"; // myArr now [1,2,3,"pears]

You probably also know how to access the properties of an object via dot notation

var myObj = {
name:'slocodemonkey',
location:'california'
};

console.log(myObj.name); // logs 'slocodemonkey'
myObj.location = 'USA';
console.log(myObj.location); // logs 'USA'

So the trick for a lot of people is iterating through arrays with objects in them, but this is a critical skill, especially when you start working with JSON.

We can iterate through an array with a for loop.

for(i=0;i<myArr.length;i++){
console.log(myArr[i]);
}

We can iterate through an object with a for-in loop

for(var key in myObj){
console.log(key + ':' + myObj[key]);
}

Note that we have to use bracket notation with a dynamically named object property. Also note that ‘key’ is a good variable name in a for-in loop, but not required. I could call it ‘pizza’ and it would still work.

Now let’s say you have an array that contains an object. This is where a lot of my group members freak right out.

var omgOhNo = [{val:'foo',secondVal:'bar'}];

Well, in this case, our array only has one index position, which is 0. So if we do something like

console.log(omgOhNo[0]); // logs [object object]

What’s important to remember is that our arr[0] is just an object, and we already know how to work with those. It’s just an object, that happened to be stored as an array element.

for(var i=0;i<omgOhNo.length;i++){
for(var key in omgOhNo[i]){
console.log(key + omgOhNo[i][key]);
}
}

Obviously, we wouldn’t write a loop to handle an array with only one element, but that loop would work even if there were ten objects inside that array.

Sorry for the long-winded response. I hope it helps!

The best thing to do is pop open this address: http://labs.codecademy.com/#:workspace

Then just create an array with objects inside it, and practice iterating through the array and getting the key / value pairs out of the objects. Then try setting them. Do it for as long as it takes to really get the concept. This will teach you WAY more than reading a book.

Then go back and tackle the record collection.

Good luck!


#6

Me personally: one day I’m in a zone and make stuff happen and other day I’m like I have no idea what I’m doing. When I’m not coding behind my computer, I read everything about the problem (usually the things on first page results Google) I’m facing and take a step back and try not too hard to find the answer directly. When I’m really stuck I ask friends, in the chat here or on Stackoverflow. Almost all the time, sooner or later the dots connect and you think like: ow, it actually makes sense. And yes reading books helps, because the better your fundamentals are, the easier you can work on real life problems. I found Learn Javascript Visually helpful https://www.kickstarter.com/projects/programming/learn-javascript-visually