Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

Monday, March 10, 2014

Links to the past/Linux, Baby!

     In the grand wisdom that is Codecadamy, I did and also did not finish the JavaScript course. Turns out they have two courses, and the second one is only half way through by the end of the first one. So, that was slightly disheartening. I got over it in typical heroic fashion. I'm afraid I don't have much for y'all today. I was busy putting Crouton onto my Pixel. Gotta love that xfce sweetness. Now I have the Sublime text editor of awesome and Brackets for HTML/CSS because it does live preview. There's a Chrome ext. called Tailor, and it's a port of brackets, but I didn't have any success with it on ChromeOS. Then again, I'm on the dev channel, that might be why. So, that's what I was up to today. Getting all the things done. I did do some more .js, so that's a bonus. The first is a retrospective. I happened to see some of my code from a few months ago, and it was interesting to me in the ways the that the format had changed. I saw this:

var isEven = function(n)
{
    if (n % 2 === 0)
    {
        return true;
    }else
    {
        return false;
    }
};

The way that I would do it(if I was to use an <if/else> and not a ternary operator) it would look like this:
var isEven = function(n){
     if(n%2===0){
         return true;
     }else{
         return false;
     }
};

I never realized hov spread out my code was. Yes, I know it acn be more compact, but this is the easiest way for me to read it.

     The other thing we have is this:

//using typeof in <for/in> loop to print what we want.

var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for (var i in languages){
    if(typeof languages[i]==="string"){
        console.log(languages[i]);
    }
}

     How cool is that?? using a <for-in> loop and the <typeof> method, we're able to print out just what we want and ignore the rest. That could be super useful. Ok. Well kids, this a rather short post today, but fret not mein Kinder, ther will be plenty for you on Wednesday! See y'all!


--Martin

Wednesday, March 5, 2014

Chippy Animation/New Method For You In There.

     Rejoice! There is HTML/CSS today! Olé!  I am here at the house up at way too early an hour for my liking, but I'm up and done with the necessities of the day, so let's get this going. You'll be happy to know that I'm listening to something else besides GrooveShark. I found some free ChipTunes albums thanks to MakeUseOf. They proved themselves worthy of my followingness. Followingness is now a word by the way. I didn't have any coffee today. Yet. So, if you see any grammar errors, blame it on that. I'll probably get a water later. 

     

HTML/CSS

     Animations! That's what we're working on today. It's a simple animation, really. We're just going to do model of the solar system. Sadly, there doesn't seem to be a cool scientific name that goes with that. Anyhoo, to start out with, it's just the sun and earth. I'll see if I can get more in there afterwards.

     The first new thing is this:

#earth {
     /* Style your earth */
     position: absolute;
     top: 25%;
     left: 25%;
 }

The %'s make the image in the earth tag adhere to a grid. It's much easier to use the % when you just need something in a general area. You can still use the <px> method if you need more accuracy.
The next new thing is:

box-shadow: 0 0 64px 10px white;

That's a line of CSS. Box-shadow does pretty much what you would expect. It gives a shadow to any element that you want. Here's what Mozilla tells us about the box-shadow's parameters:

"1st <length>
Specifies the horizontal offset of the shadow. A positive value draws a shadow that is offset to the right of the box, a negative length to the left.
2nd <length>
Specifies the vertical offset of the shadow. A positive value offsets the shadow down, a negative one up.
3rd <length>
Specifies the blur radius. Negative values are not allowed. If the blur value is zero, the shadow’s edge is sharp. Otherwise, the larger the value, the more the shadow’s edge is blurred.
4th <length>
Specifies the spread distance. Positive values cause the shadow to expand in all directions by the specified radius. Negative values cause the shadow to contract."
The 4<length> also is the part that deals with brightness.

     You know things are about to get fun when the lesson starts off with this:

"The next couple of steps are going to be a little tricky, so hang in there."

Let's see what they're about to throw at us.

     
     The first thing we had to do was to surround the <img> tag with a div with the id "earth-orbit" I guess that means we're about to get some movement here! I was right! We have movement! Earth is spinning away because of webkit stuff. I won't really get into that right now, but it's pretty much what moves things around. To see the finished code/product plus a few planets, go here.  I'm pretty sure that they're going the wrong way, and I know that the scale for size and distance is off. I just did it for fun. 

JavaScript


Today, we get to deal with a new method called <hasOwnProperty> it gives back a boolean response as to what is in an element. Search for what you think is there, and it'll tell you. Example:

var myObj = {
    // finish myObj
    name:"john"
};

console.log( myObj.hasOwnProperty('name') ); // should print true
console.log( myObj.hasOwnProperty('nickname') ); // should print false

This might seem stupid now, but when you're dealing with thousands of lines of code, where the object is more complex, or it's not easy to be sure, this is what you want. It'll tell you straight up!

And here we have a use-case in an <if/else> statement:

var suitcase = {
    shirt: "Hawaiian"
};

if(suitcase.hasOwnProperty("shorts")){
    console.log("You remembered to pack!");
}else{
    suitcase.shorts="blue";
    console.log(suitcase.shorts);
}

So, as you can see, it's got quite a lot of good uses!
I've already gone over the <for> loop and I might have talked about the <for-in> loop? Not sure. Let me recapitulate: the <for-in> loop is like the <for> loop, but it's more simple. Here's a comparison:

The <for> loop that would print out the contents of an array:

var array = ["hey", 2, true,"dude"];

for (var i = 0; i<array.length; i++){
             console.log(array[i]);
}

It's cycling through each element of the array, and printing it out to the console.

And here's a <for-in> loop doing the same thing:

var array = ["hey", 2, true,"dude"];

for (var i in array){
             console.log(array[i]);
}
notice how less verbose it is.

Now, you might be thinking that having these two loops that are practically the same is just stupid. Why not just have one? Actually, if you look at the first loop, you'll notice that it has a specific end-point. It's only going to go as far as the end of the array, and when you're using it to do something else that needs a specific end-point, like creating a long list of numbers that doesn't crash, that would be the <for> loop. The <for-in> loop works better for these types of use-cases. Something where you're worried about the properties in an element and you need them printed out, numbered, or anything else you can think of.



     That'll about do for me today, folks! I had a lot of fun with this one! There was a lot of new stuff going on. I like new stuff. See y'all on Friday!

--Martin

Friday, January 24, 2014

Google Is Dead/Branching Out

     Well, Google went down today. That was fun. For a minute there, I was going to have to use spotify or some similar service to play music, but music is working, G+ is still being cray cray. No coffee for me today, I might make a tea at some point. I'm trying to stop depending on caffeine. This sudden halt to most of Google's services has made me realize that I depend on Google too much.  I need to branch out my dependencies so that when a service goes down, I won't go with it. I've got a Creedence Clearwater Revival radio going and missing my coffee already. I'm here at the house getting stuff done and ready to plug in and plug away!

     I've started some HTML5 lessons alongside my JavaScript lessons
I know that .js, HTML, and CSS go hand in hand in hand. So, I did this lesson series. It's pretty short and easy. HTML is short and easy for the basic stuff I need it for. Though, I do have some HTML5 game ideas floating around. 
But, that's enough HTML for now.


     It seems that the dev channel on my Chromebook didn't like Code Academy. I switched to the beta channel and I no longer have cursor issues. So, until the next dev update, I'm staying on beta. I'm still working on <while> loops and such. Not much more to go, so let's get this going!

     ReferenceError: Invalid left-hand side in assignment What? ugh... I keep getting this. Darn Reference errors... Well I figured it out. I was doing a for loop wrong, but not wrong enough to get a red thingie on the left of the editor. The <do/while> loops are pretty nifty. The name of the variable that I'm working with is called getToDaChoppa. Can't say they don't have a sense of humor.

     Hey, look at that! I passed the lesson review first time around! That doesn't happen too often. Looks like these studies are actually helping me out here. This is a good note to end the week on. I'll be starting on a dragon-slaying minigame next time. See y'all next time!
--Martin




Monday, January 20, 2014

New Font New Lessons

     Well, I'm at the coffee shop. I wasn't planning on going here, but the internet is down. I mean really down! the line got cut somewhere between our neighbor's house and our house. So, the company is going to be digging around fixing all of that for a while. So, here I am getting stuff done and having a good latté. I got a little sidetracked by some questions about my Chromebook Pixel, and it does cut quite the figure, so I can understand people asking about it. I'm back on track now. The music for today is: The Crystal Method. I'm not really sure how I came across it, but I put in my Google Play Music a bit ago, and voilà! Cool music! I'll probably throw some Pink Floyd in there too. The radios are just playing their two most popular songs according to them: Another Brick In The Wall, and Wish You Were Here. It makes me want to hear more than just those two, but enough chatter! Time to plug in and plug away!

     Today, the lessons are starting out with arrays. From some Googling and just general info received from people, I know that arrays are going to be very useful. So, I need to keep my head up.

     Yay! Loops and arrays! Check it out:
// Let's print out every element of an array using a for loop

var cities = ["Colorado", "The Grand Canyon", "A lot of Europe", "NYC because Gotta go at least once, I suppose.", "Japan", "Damn Yankee States", "New Orleans"];

for (var i = 0; i < cities.length; i++) {
    console.log("I would like to visit " + cities[i]);
}

It kind of had me there for a bit until I realized they weren't trying to print numbers, they were printing the elements in the array and calling them in order by number.
I added in the elements in the array. As you can see, I want to visit a profuse amount of places. Of course, this isn't an exhaustive list.
     Wow. These people don't even remember what they are saying! They said, and I quote: "Remember, arrays use zero-based indexing!" The Image they show with it? <var arrayName=[1, 2, 3];> Um... no! That is NOT zero-based indexing! This is: <var arrayName = [0, 1, 2];> Duh. It's not like they don't know this! I learned what zero-based indexing is from them; they just can't show their work correctly all the time. Also, it would seem I should have known that if I have a <for> loop, you just run it. You don't need to print it to the console. I noticed that it was printing out the array in order going down, and then having it inside the brackets with the quotes and everything. I knew it shouldn't print twice, so I checked the forum, and they were all just running it. Well, now I know that too! 

     I always forget to put a space at the end of the quotes when it is concatenated with anything else. I better get that under control. Creating code with a solid simplicity also needs to be correct, of course. So, I better work on that. Don't want to get a job somewhere and then start doing crap code, do I? Of course not.

Today has been a win for me, I got a lot done, learned a lot of useful stuff, there was only a little bit of stupid. Winning! See y'all Wednesday!

--Martin


Friday, January 17, 2014

Loop Loop Loop... Loop.

     I stayed home today so that I could get some work done around the house. So, no latté or anything for me today. I'll probably do a french press if I find some beans that aren't dead. But I have the music! I didn't really feel the Tomahawk album, so today I'm sticking to some awesome Pink Floyd.
     I've done loops quite a few times and yet, I still can't remember how to do them from memory, so before I do anything in my lessons, I'm going to practice loops until I can remember how to do them from memory. And apparently having:
 for (var i=8; i<120; i+12){
    console.log(i);
}
will freeze the browser. I got it wrong, I know. Now I know not to do <i+12> or anything like that. The correct version:
for (var i=8; i<120; i+=12){
    console.log(i);
}
works. Notice the <i+=12>. Like I said, I need to work on my loops. Onwards and onwards and onwards! Heh. Loop.
Oh look a FizzBuzz. That's got loops. Yay! Good Thing I'm prepared. I like FizzBuzz tests.
     Good Lord the console is so specialized here that even when the code actually works, they just say that it wasn't what they wanted. I should at least be able to see what I did  get to work.
I'm really liking the <for> loops.  It's pretty handy to just have the one line that does so much work.

     So,when you are defining a function, you can use <return> with a string, but otherwise you need <console.log> I guess? OK.
"unreachable 'return' after 'break'." What does that even mean? Here is a part of the code:
var getReview = function (movie) {

switch(movie){
    case "Matrix":
    return "good trip out";
    break;
    case "Princess Bride":
    return "awesome date night movie";
    break;
After each break I got that error,  but the code worked and I passed the lesson. I checked, and that's what they wanted. Why would they want something that has errors to be the answer?
But hey, switch statements.I love their ease and simplicity. Simplicity over all, y'all!  The more I learn, the more I like this language! I'm sure that is true far anyone that likes a computer language. I just feel like I've really hit my stride with my lessons! I've just got to soldier on and get done so I can get a decent job somewhere nice!
I'll see y'all Monday!
--Martin