Javascript cribsheetery

So to declare either variables OR functions (and I assume, later, objects? unless it turns out they’re the same – they might be the same), it begins with var variableName = and if it’s a simple variable, it behaves predictably, and moves on to var variableName = (5 * 2). If it’s a function, this entire lovely curly brace structure needs to be set up:

var functionName = function (parameter) {
    console.log(parameter * 2);
};

and then to call:

functionName(5)

where 5 is the parameter of the function functionName.

Ok. Rock and roll. Movin’.

Lessons 29, 30 & 31

Hey-o, conditional statements! I breezed through a bunch of lessons because we’re getting back to the kind of programming that I have already learned quite well. For the novice, my blog will not be as helpful as it may have been for previous lessons. The issue is that I am super ready to finish LPTHW!

For Exercise 29, he introduces conditional statements, the results of which I could see as I typed them in.

people = 20
cats = 30

if people < cats:
print "Too many cats! The world is doomed!"

if people > cats:
print "Not many cats! The world is saved!"

Basic less-than/greater-than operators – clearly the world is doomed! That’s about all there is to this exercise. (Ed: T.I.L. that less than and greater than symbols have to be inserted into with & l t ; and & g t ;, respectively! Awesome!)

In Exercise 30, it’s the same thing but introducing, hmm, subconditional statements you might call them? Rather than just if and else there’s also elif which takes its place before the final else in which you can make a further conditional statement within the if statement. This has been one of the handiest things I have learned (though I learned it long before this lesson) in Python, and it has analogues in every other programming language as well. Very cool.

In Exercise 31, the conditional statements get wrapped up in a “game” in which your player gets to choose from various options, to either be driven mad by Cthulhu or ripped apart by a bear. HMMM, which would you choose?? The purpose of this lesson is to start getting the student comfortable with nested if-statements. Old hat for me – I need to move a little faster!

With that, I think I need to go through these lessons a little more quickly than I want to blog about. I will write another blog when I come up against something that is truly new to me. I know Zed Shaw has challenges for me yet!

Lesson 28

Hoo-woo-woo-wow, Lesson 28 was a zipper, but I imagine that is only because I took Discrete Mathematics about a year ago, in which boolean logic was beaten into us : )

I thought this truth determination was especially funny to find. Can you find the correct truth value?
"chunky" == "bacon" and not (3 == 4 or 3 == 3)

I got them all right! Moving on, breakneck pace my friends! But really, I want to get back, so badly, to “copy this in & solve a problem.” I looked ahead a bit & I think the next one is like that. Oooooh I hope!

Lesson 26

AHH.  Feels good to be back to ordinary python (though I now have much more planned for heroku/django!).  Today I wanted to punch out a quick lesson since it’s been a few weeks for a Learn Python the Hard Way, and I want to maybe finish the book by the end of the year.  Here’s exercise 26, a debugging exercise. Excellent. *cracks fingers* Ok! First, I copied in the required “code” from here into nano & immediately set to correcting all the tiny errors, like missing parentheses, spelling errors (not that those matter too much in this exercise but pedantic habits die hard), and inconsistencies with function naming/calling. I knocked this out in about fifteen minutes. Exciting!! I should do another, even though it’s thanksgiving eve & that bathroom isn’t going to clean itself 🙂 ok but yeah just one more for real, this didn’t quite scratch the itch! and then I’ll get to the bathroom, jeez DAD LAY OFF

Edit: OH MY GOD THE NEXT EXERCISE IS TRUTH TABLES! HOORAY!!

seriously, I love truth tables.

Lesson 21

Moving along, here! This lesson was mostly a refresher on function ordering, for me. At the very end, there is an extra credit portion where he recommends resolving a nested set of functions by hand before running the program.

print "Here is a puzzle."

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"

I knew that it was necessary to resolve inner parentheses before moving on to the next inner set of parentheses as just a giant composed function, but once I got to the non-commutative operations (ie, subtraction and division, in which order of operation matters) I flubbed it up a bit, and came up with a different answer than when I ran the program. Then I examined it a bit, and with a bit more analysis (that’s an overly heady word for it, ha ha!) in the form of the following:

age = add(30, 5) #35
height = subtract(78, 4) #74
weight = multiply(90, 2) #180
iq = divide(100, 2) #50

and then resolved the commutativity issue, I came up with the same answer that he did! woo-hoo!

Woo-hoo! Another lesson down!

Lesson 18

In this lesson, Mr Shaw introduces the idea of creating one’s own functions. Fortunately, I have plenty of experience with this and this exercise fazed me not much at all. It takes a moment to get my bearings considering what gets passed through to the defined functions, but it’s really not bad overall. I do see the difference again between Pythons 3.3 and 2.7 again, though, in the way that the arguments are inserted in a print statement via the %r rather than just exiting briefly out of what is being printed and just throwing the variable.

3.3 (what I am used to):
print(“The variable goes here:”, variablename,”.”)

2.7 (LPTHW)
print “The variable goes here: %r.” %variablename

Passing the parameters from the command line as arguments into the defined functions seems to make sense! **(Edit: nothing is passed from the command line to run the program, here. The arguments are defined within the program, not without.) Since a function just does whatever you tell it to (I suppose the same could be said of literally anything you punch into a future program), its structure and formatting does not elude me, especially since we made lots of them when I was knee-deep in 3.3.

And how exciting! He has a big red warning, “don’t worry if you are confused,” well, I am not! Yay! I know I will keep having other troubles down the line, but this one is pretty ok!

The Common Student Questions area advises me to try to break if it I seem bored 🙂 so that is what I did! I passed an argument into a function without using it in that newly defined function, and it wigged out – perfect! I don’t think 3.3 cared if you used the parameter/arg/var passed in, but 2.7 does, and that is sensible and easy to synthesize.