Showing posts with label ternary operators. Show all posts
Showing posts with label ternary operators. 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

Monday, February 17, 2014

Beautiful Weather/ Not A Walk In The Park

     It's a beautiful day here in Texas. I had to take a break from my studies and go for a walk to enjoy this gorgeous weather. One of my friends told me that Katie Herzig was giving all of her music away for free today. So, that's what I was listening to. It was pretty good. I actually got my studies on a roll today. So, this is actually going to go out on time! No coffee today. I'm awake enough from all the amazing weather. So, time to plug in and plug away!


HTML/CSS






      I've been doing HTML just long enough for that picture to make my eye twitch just a little. Ever since I started learning HTML I've been writing these posts in HTML and I'll get little error messages if a tag isn't closed, or something. So, I'm more perceptive to tags lately. Mainly because I don't like the error messages. I'm pretty sure that I don't really care for the <nth-child> tag when it comes to calling a specific element. I mean, what's wrong with class or id? Although, I suppose that maybe some tags can't have those in there. So, maybe that's when they're useful. Hopefully.
I earned a badge! Olé!





JavaScript




      JavaScript. So useful, and yet so daunting. I seriously need to start doing some real world applications to have some real knowledge under ye olde metaphorical belt. I have the most basic ideas of .js, but some of the more esoteric practices can be overtly foreign on such a frequent basis that the only way to retain knowledge is to do the lessons over and over and over and over... As long as it actually helps me attain something in the end, I suppose it's worth it. The code that I'm starting with today is pretty much just simple math. Which should be easy, except that it isn't always easier said than done











 If you want to do cool code snapshots like this, go here.  It's called Marmoset.
      Anyhoo, the age bit was OK. I just had to do an <if/else> statement. Although, I'm sure I could do a ternary operator and have it all done better. I'll try that next time I need an <if/else> statement.
Seriously, remembering the names to these things can be hard! I tried three different ways before I got this. Like I said. remembering things in .js is hard sometimes! Remembering all the operators and objects and whatnot. I'm going to eventually remember them all, but it isn't exactly a walk in the park. Not that anyone said it would be. OK. That's the end for today.
See y'all on Wednesday!


--Martin

Wednesday, January 8, 2014

What The What!

     Ever since the beginning of this course, I've been working with if/else statements and conditionals. It would seem that thing are about to take the next step. Now I'm starting on switch statements. Olé? I'm actually pretty happy with my rate of studying. I just have to keep on plugging in and plugging away. Ok, so I'm working away with the switch statements(Which are awesome BTW) and now they're saying there's another way to do it called Ternary operators. According to el Goog, 'ternary' is an adjective meaning composed of three parts, or in mathematics, means using three as a base. Now to find out if that is what it means in programming. They say they are an ugly name. That's kind of dumb. I like the sound of the name. Ternary. Sounds like it's about a bird or something. But I digress, back to the code shtuff. The Offspring is my soundtrack today. I'd never heard of them, but my friend suggested them, and I am really liking them. My drink is some black coffee. Don't want to have the same type of thing all the time, right?
What the what! I know <if/else> stuff; now ternary operators can do that in one line?
Why did I learn <if/else> ? It better be because sometimes it's better to use one over the other.
Here's what I mean:
var x = 7;
var y = 4;

if (x > y) {
  result = "good job";
}
else {
  result = 20;
}

//Below is the above code written using the ternary operator
result = x > y ? "good job" : 20;

See what I mean? 
If I find out there's something after this that's even simpler, I'm going to be upset with these dudes.
Fist pump! I'm getting these down easy!
Maybe that's why they started with <if/else>? I hope that they had a plan when they did this.
So, the good news is that I can read the code they have set up for me and see what it means, the bad news is that now when I read anything and see punctuation commonly used in .js (<>?/+===) for instance, I start reading in 'code mode' when it's not code, and I get confused for a sec. That will probably go away after a bit. I hope?
That's all for me folks. I got a lot done today! See Y'all friday!
--Martin