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);

3 Comments:

At June 7, 2014 at 6:14 PM , Blogger Unknown said...

Hi Kaeley,
I love the name animation! Learning code must make you feel very powerful - you can make all sorts of things happen. Looking forward to seeing more links!

 
At June 7, 2014 at 6:41 PM , Blogger chelsea.yuill said...

Hey Kaeley!

It makes me think of a 21st century digital poem; a new kind of language to explore. Is there anything specific you're interested in exploring with Javascript?

 
At June 7, 2014 at 10:12 PM , Blogger Kaeley said...

Chelsea,

I'd ideally be able to use my new knowledge of JavaScript to build websites or web apps, so I'll explore that once I've got the basics down.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home