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

5 comments:

  1. To ensure that you have proper coding, be sure to declare the result variable using var.

    Also, you should try to use single ticks for the string values instead of double:

    var x = 7,
    y = 4,
    result;

    result = (x > y) ? 'good job' : 20;

    ReplyDelete
    Replies
    1. I always used a double ticks because there might be a word like(they're)in there and the tick in they're would end the string, right?

      Delete
    2. Also, I don't need to say var twice? Just do a comma and hit enter? That seems like it'd be useful. :-)

      Delete
  2. Your post isn't quite right, the conditional operator (aka inline-if) is a *type* of ternary operator. It isn't called *the* ternary operator. Few (if any) languages have any ternary operators other than the conditional operator, so this is likely where the confusion originates.

    ReplyDelete
    Replies
    1. So, there's no such thing as THE ternary operator, there's just different types, and what I was calling the ternary operator is actually a conditional operator which is a TYPE of ternary operator?

      Delete