Skip to content

Instantly share code, notes, and snippets.

@Cheezmeister
Last active December 1, 2016 08:48
Show Gist options
  • Save Cheezmeister/e9462ffc3d26095190f93af13d0d4e2b to your computer and use it in GitHub Desktop.
Save Cheezmeister/e9462ffc3d26095190f93af13d0d4e2b to your computer and use it in GitHub Desktop.

Hey mentor,

I was working through an assignment last night and came across a bug. I'm so confused!

The text in the alert is showing up as "undefined" instead of either "A Unicorn", "A hug", or "Fresh Laundry" and I'm not quite sure what's going on. Can you point me in the right direction?

-Student

<button id="btn-0">Button 1!</button>
<button id="btn-1">Button 2!</button>
<button id="btn-2">Button 3!</button>

<script type="text/javascript">
  var prizes = ['A Unicorn!', 'A Hug!', 'Fresh Laundry!'];
  for (var btnNum = 0; btnNum < prizes.length; btnNum++) {
    // for each of our buttons, when the user clicks it...
    document.getElementById('btn-' + btnNum).onclick = function() {
      // tell her what she's won!
      alert(prizes[btnNum]);
    };
  }
</script>

Remember that your script runs in one shot before you start clicking buttons. So, by the time you click a button, what is btnNum set to? You can set a breakpoint to find out, or use console.log if you prefer.

It turns out we need to "capture" the value of btnNum. You can do that in several ways. Or going another direction, you can have your click function look at the button it was triggered by to figure out which number it is.

Hey mentor,

I am starting to understand associations, but I'm still confused about through. What's the practical difference between a has_many and a has_many :through? What about has_one :through? When do I use them?

-Student

I'm not sure. (mentor) might know. In the meantime we can also check the doc at http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many and see if it makes sense.

Hey mentor,

Thanks for the lecture yesterday, it really helped me get motivated again. I've been working through this assignment about databases and I'm confused about "SQL injection." What does that mean and how does it work? How do I prevent it in my apps?

Thanks!

-Student

It's a common type of attack hackers use where "evil" SQL code is put into user input. The hope is that the coder is lazy and puts them directly into his SQL commands, which allows the attacker to do anything the vulnerable code h as permission for in the database. Not a good thing! To avoid this you shouldnever build SQL statements out of strings, especially with input that comes from users. Instead use parametrized statements where you specify which values are inputs - the SQL engine will make sure they are only inserted, never executed.

Hey mentor,

I'm really excited about starting on learning Angular. I'm having a problem understanding what angular is. Up until this point we've used jQuery, which we've called a library. Now we are using Angular, which is a framework. What's the difference? What are the drawbacks/benefits of using a framework like Angular vs a library like jQuery?

-Student

Glad you're excited. Angular is pretty snazzy. I wrote a few words about the difference between libraries, modules and frameworks. There's no formal definition of either, but the things we call frameworks are "heavier" than libraries. Using them takes a certain level of commitment and they tend to be specialized. For example, Angular is great for e-commerce and dashboards, but less so for games or wikis. They don't play nice with other frameworks; you can rarely use more than one without weird side-effects.

Libraries are more like ingredients to a soup. You can toss them in and (unlike soup) rip them out if they don't work out. They help, but they won't make the soup for you. They're simpler and easier to pick up. They almost never fight with one another. It's usually okay to use several libraries with a framework.

Does that help?

Hey mentor,

I hope you're doing well. I'm really enjoying learning algorithms, but I'm pretty confused by this Big O Notation thing. Can you explain how I'm supposed to figure out which notation my algorithm uses?

-Student

Don't worry; Big O Notation is pretty confusing. It's a simple idea but the notation throws everyone for a loop, myself included. It has a very precise mathematical definition that we can go over if you're really curious. For practical purposes, you can think of Big O tiers as an approximation, sort of like thinking in terms of orders of magnitude instead of exact numbers.

Do you have a specific algorithm you want to go over? Entire courses are taught about classifying algorithms, so I don't have a simple answer. Generally we want to analyze its for loops, how they are nested, and if/how/where any recursion happens.

Hey mentor,

I'm having a really hard time with some CSS. I've been given a PSD in an assignment. I know what the document should look like, but I'm getting this instead.

Can you help me with this? Here is my code!

-Student

I'm not sure. CSS isn't my strong suit. We can ask (mentor). At a glance it looks like your content isn't boxing into .pricing and .pricing-tiers like we want. Sometimes using the float attribute can cause this sort of thing, have you tried removing it to see what happens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment