Sunday, June 8, 2014

Getting Loopy.

The biggest problems with loops is that if you write them wrong, they never stop, crashing the browser. I've done this a lot lately. As in almost every exercise. The syntax of the do/while and for loops is confusing. I've had some exercises where I can't figure out what to write, and I feel like sometimes I haven't been taught how to write the code properly, even though other people understand how to do it. Hopefully it gets hammered into my head while I write the Dragon Slayer game that's up next in my lessons.
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;
while(slaying){
    slaying = false;
    if(youHit) {
        console.log("Congratulations, you hit the dragon!");
        totalDamage += damageThisRound;
        
        if(totalDamage >= 4) {
            console.log("You slew the dragon!");
            slaying = false;
        } else {
            youHit = Math.floor(Math.random() * 2);
        }
    }else {
        console.log("You have died.");
        slaying = false;
    }

}
This one was a fun exercise. It uses a random number generator (Math.random) that is rounded to the nearest whole number (Math.floor) to determine total damage to a dragon. If you miss once, you lose, since dragons are super powerful. Take a look at it here with a browser's console.
There's no real interactive elements yet, but I think that's something that's more complicated, so they haven't touched on how to really do it yet. I'll keep learning more JavaScript and being able to do cooler and cooler things.

Friday, June 6, 2014

Functions and Fun

If I want to eat cake, I can get JavaScript to say it. Like this:
var foodDemand = function(food)
{
    console.log("I want to eat" +" " + food);
}
foodDemand("Cake");
Functions are pretty cool, they have the ability to assign an identifier to a part of a code and then pull that code up later, with the ability to change parts of it. Basically I'm telling the computer to say I want to eat something, and the part where it says food is the part that can change depending on what the user inputs. It's viewable here with a web browser's console.

I took a bit of a break from the basic lessons to see if there was anything else that was cool to do with JavaScript on the Codecademy site, and there definitely was. I made a really neat animation of my name with just a little bit of code and a lot of functions.
var red = [0, 100, 63];
var orange = [40, 100, 60];
var green = [75, 100, 40];
var blue = [196, 77, 55];
var purple = [280, 50, 60];

var myName = "Kaeley";
var letterColors = [purple, blue, green];
if( 10 > 3) {
    bubbleShape = "circle";
}
else{
bubbleShape = "square";
}

drawName(myName, letterColors);
bounceBubbles();
You can take a look at this here. It's really cool!
And my last little adventure here was a rock, paper, scissors game. There was one point where I was getting the code wrong, but I could not figure out what it was that was making it so wrong, but it turned out that I had gotten the math wrong. Whoops. This program uses a random number generator and either rock, paper, or scissors applied to a span of numbers to play rock, paper, scissors against the computer. Code below, check it out here.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function (choice1, choice2) {

    if(choice1 === choice2) {
        return "The result is a tie!";}    
    else if(choice1 === "rock") {
        if(choice2 === "scissors") {
            return "Rock wins!";
        }
        else{
            return "Paper wins!";
        }}
       else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "Paper wins!";
        }
        else{
            return "Scissors wins!";
        }} 
        else if(choice1 === "scissors") {
        if(choice2 === "paper") {
            return "Scissors wins!";
        }
        else{
            return "Rock wins!";
        }} 
        } ;
        compare(userChoice, computerChoice);

Thursday, June 5, 2014

Learning the Language and First Attempts

I've been using Codecademy to help me learn the basics of JavaScript, which is thankfully presented in an easy to understand and easily digestible format of many small lessons and some repetitions thrown in to cement my new knowledge. I've got lots to remember: strings, substrings, solving math, if/elses and that's just to start. My first small "game" looks like this when not in action:
// Check if the user is ready to play!
confirm("I am ready to play");
age = prompt("What's your age?");
if (age < 13){
    console.log("You can play, but we take no responsibility for the content");
}
else
{
    console.log("Have fun!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt ("Do you want to race Bieber on stage?");
if (userAnswer === "yes")
{
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else
{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
var feedback = prompt("Please rate my game out of 10!");
if (feedback > 8)
{
    console.log("Thank you! We should race at the next concert!");
}
else
{
    console.log("I'll keep practicing coding and racing.");
}
Doesn't look like too much, does it? It's basically a sort of choose your own adventure game, where what it typed into the prompts that pop up on screen change what happens next. This is word for word what the tutorial instructed, so I really doubt I would write something about Justin Beiber on my own. Take a look at it here. One thing to note about this link: console.log doesn't show up as html text on a webpage, so view it through a web browser's console, like firebug for firefox.
Remembering how to write the if/else conditions was the trickiest thing for me to remember, it all needs to be written in a specific way so that it'll work and just missing a tiny element makes the whole thing not work. Thankfully the tutorials I'm using displays all the errors so I know where I'm messing up.
I'll have to put up a link so that the code can be tried out.
On to more complicated things!

JavaScript and a Brief History of Computer Programming

JavaScript "is the Netscape-developed object scripting language used in millions of web pages and server applications worldwide" (Mozilla). JavaScript has been around since 1995, and was developed in 10 days by Brendan Eich, starting out as Mocha. Trying to appeal to multiple levels of experience with programming, JavaScript “was to be a light, friendly version of C with simpler semantics” (Charles Severance), with thebasic syntax [being] intentionally similar to both Java and C++ to reduce the number of new concepts required to learn the language” (Mozilla). JavaScript is a very common language to find on web based platforms, each web browser has interpreters that help it understand JavaScript, since its association with the browser makes it one of the most popular programming languages in the world” (Douglas Crockford 2).

JavaScript's endurance is only possible thanks to some “improvements [during the late 1990s] through the [ECMA] standards process” (Severance) which helped it be able to endure to the early 2000s, and with some further improvements “it became fast enough and good enough in 2004 and 2005 to beget the Web 2.0 revolution” (Severance). It has been able to grow and change with the development of web browser technologies, without changing its syntax. This allows those who know the language to continue to use JavaScript without huge amounts of time spend on relearning it and instead being able to explore the new possibilities offered by the new technologies present in web development. Today, JavaScript is present in very well known web applications and its language allows “highly interactive user interface functionality [to be] moved into the browser to create increasingly rich desktop-like experiences in applications such as Google Mail and Google Maps” (Severance).

MATERIALS
Creating apps, games and other web applications require two things: knowing a programming language and having a computer. There are several options to choose from when considering a programming language to use for app/game development with the most popular ones being C, C++, C#, JavaScript, Java, PHP and Python.

While I did consider these other languages, I decided to start out with learning some JavaScript due to its relative ease of learning and widespread usage, being a small, lightweight language [...] designed for easy embedding in other products and applications, such as web browsers.” (Mozilla) All modern browsers understand JavaScript, and is used in a large amount of web based applications, creating more dynamic web pages and is also used in game development. I wanted to learn JavaScript for the purpose of this project, but the benefits of knowing it for web development make it ideal to learn.

C is another widely used option and was my other main choice of language to use. Syntax from C forms the basis to its cousins C++ and C#, both being more advanced versions of C, while also helping to shape the base syntax of JavaScript. “C has been around for several decades and has won widespread acceptance because it gives programmers maximum control and efficiency” (Marshall Brain), but is also more complicated than JavaScript, being a much more heavyweight language, with many more uses than mostly web applications.

Even though C is apparently very simple to learn, and does form a lot of the syntax of JavaScript, I decided that JavaScript was a more useful language for me to learn. The ability to transfer my learned skills into something I may use in my professional life, instead of just learning for this project and just forgetting it later. The availability of tutorials for JavaScript that integrated with the building of websites at further skill levels also influenced my decision to learn JavaScript.

REFERENCES