While I while away the javascrours

This post is mostly reference, with some thoughts on structure at the end!

Pythonic

while i < 3:
    print "still less than three!"
    i = i + 1                       # i += 1

Javascripture

var i = 0;

var whileloopfunction = function (counter) {
    while (i < counter) {
        console.log("a way more complicated way to do this, it seems!");
        i++;
    }
}

whileloopfunction(3)

Not sure exactly how to simplify this. The reason that I’ve made the parameter counter is because it seems that I must create a function with a parameter so that it can be called.

Ah – I just tried it taking the counter param out altogether and made the while loop’s condition (i < 3), calling whileloopfunction() with no argument and it works just fine. Experimentation! Sliiiightly simpler than previously, though it was a good lesson all the same.

This is looking so much to me like a simplified object, at least, simplified from the perspective of Python. This statement may confuse people – after all, Python is a more human-readable language, right? Well, its objects are a mess if you ask me, though the rest of it is lovely. I was once told that you start to really understand OOP when you learn a second OOP language. I think that is becoming true. What a relief – it's been frustrating not knowing.

Ah, one last thing – I'm pleased to present these posts to you, now written in Markdown instead of the hammer-where-you-need-a-toothpick html formatting! Thanks, WordPress!

Advertisements

javascript and python’s range()

So in python, you can use a function called range() to (conventionally – I know there are other uses) easily iterate over, yknow, a range of numbers or through a list/array/yaddayadda. It works like this:

for i in range(13): # you can also limit it on the lower bound, a la range(7,13), 
    print i         # but be ye wary of yon fenceepostee

While going through some javascript tutorialling, I found a typical learner problem that I found later that I’d been approaching the wrong way, but if you’ll bear with me & restrain thyself from punching angrily through your computer/rotary telephone, COME WITH ME ON THIS JOURNEY:

For a Rock Paper Scissors game, lesson 11 or 12 of the Functions lesson of Codecademy‘s javascript class, it asks the student to use a randomize function (Math.random()) to call “rock” when the first third of the number, “paper” when the second, and “scissors” when the third. My FIRST thought was to use a range function, like if i in range(.33) and if i in range(.34,.66)! So I tried a couple of different syntactial approaches that seemed javascriptey, they didn’t work, so I went a-googlin’ (how you do) and found the following solution:

Array.apply(null, Array(5)).map(function (_, i) {return i;});
[0,1,2,3,4]

I know I’m new to javascript, but that is honestly barely parseable. I’m not here to wail about things that one language does that another doesn’t, but this was a difference that frustrated me – – until (and you patient few, I know you’ve been waiting for this) I realized that there’s another way to solve this problem! And that’s the beautiful thing about programming, particularly learning different languages. I remember using this syntax in other learning situations, back in the day when I first learned Python. The resolution is to use the (javascript) format of:

if (i < .33) {
    return "computer chose rock";
}
else if (i <= .66) {
    return "computer chose paper";
} else {
    return "computer chose scissors";
}

which looks (save the curly braces, ;, & s/else_if/elif) nearly the same as pythonic syntax.

MORAL OF THE STORY: there are many ways to do many different things! fabulous!

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’.

Javascript first thoughts, mackatoots, & tutorialino

Hello again! I have been working and working and working, lately, and I have a few things up my sleeve!

First, my company, The Open Bastion, is working on tutorials in Python. Get in touch if you’d like to create with us! Note: paying gig 🙂

Next, though I know some Python, it seems that I need to keep going, so I am learning Javascript, now. Exciting! Something altogether new! So far, it seems like a friendyfriendly C analogue, based on the 50-odd pages of a C++ book I read back in the day, which makes web site and app creation a BREEZE. I know complications will come – I feel like I hear more complaints about Javascript than any other language, right now, but as with all new languages, I’m really enjoying learning the syntax. I’m doing the Codecademy interactive tutorial and it’s adorable and fast fast fast, so I feel like I am getting a lot done and learning learning! Note to self (and to youuuu!) to research later – why are semicolons sometimes required at the ends of lines & sometimes not? I wonder – is it more than just good style? With Cx semicolons are quite explicitly required, right?

Next, I’m working on a Mac, now. If you know me, you’ll know that this is, like, a huge deal for me. It’s just a work machine, AND it’s four years old, buuuuut it’s great for coding even if it’s comically bad at other things, i.e. file management (honestly does anybody EFFECTIVELY use Finder? my theory is that apple wanted to ensure that their file management system was different than windows’ [whose Explorer {directories, not internet} is superior to all others] and now they’re too bashful to back out???). The mouse pad, though, is a serious delight to use, the best touch pad I have ever operated on, and the keys are lovely to type on. It’s strange having no optical drive (it’s an Air) and the minimal number of USB ports can be frustrating, but of course, a $1300 machine is a $1300 machine, and a professional versus hobbyist (read: my perfectly delightful $600 2.5y/o windows/ubuntu machine) computer is going to be a vastly different user experience. I hate the fanboyism around macintoshes so, so much, though, that I probably will still never buy one on my own, but it seems you need to be comfortable with them to work in tech, at least in Portland. So here I am. A bit blech, a bit “oh this works really well.”

Lastly, I’m volunteering with a group called Chick Tech right now, which assigns mentors in industry to young women. It’s a very cool organization, and I can already see myself staying involved with them for a long time. I worry about this approach to solving one of tech’s many diversity problems, though – the problem, at this point, is not to interest young women in STEM, but to keep grown, adult women in tech once we are here. And here, we are, and I/we will demand equality. Jessica McKellar and Selena Deckelmann, a couple of fabulous tech luminaries whom I follow, both refer to the “leaky pipeline*,” – we can get them in, but we can’t keep them there. I think the following tweet from Jenny Thurman sums up the issue quite well.

So I will continue to think about this while I have a blast with my mentee, a motivated young lady whose trajectory I am excited to follow and encourage.

* the earliest reference I have found to this particular use of this term is from a paper from 1996, found here.