Decisions…Decisions

In my last post, I talked about if statements. I mentioned that they evaluate a statement and return a boolean (true/false). Then, based upon the result, some code is run (or not run). The examples I displayed were simple, single IF statements, but what if we have more complex questions to evaluate? Well, we can do that. One solution is the else statement…

// If/else statement 
if(thisVar === thatVar){
     // Do something 
} else {
     // Do something else 
}

As you can see from the above code sample, we can evaluate a statement as to whether it is true or false. We can then have the code do one thing if the result is true and something else if it is false. This is very handy.

But what if we have multiple conditions to test? This is where the else if comes in to play…

// if/else if statement 
if(thisOne === thatOne){
     // Do something 
} else if (thisTwo === thatTwo){
     // Do something different 
}

So we can test multiple condition statements in one code block. One tip…you might want to end you IF/ELSE IF statements with an ELSE section to act as a ‘catch all’.

// if/else if/else statement 
if(thisOne === thatOne){
     // Do something 
} else if (thisTwo === thatTwo){
     // Do something different 
} else {
     // Do this if all else fails 
}

Doing this will help ensure your code is protected and more ‘foolproof’ against any possible unknown condition statement results.

Happy Coding!

Clay Hess

If ‘IFs’ and Buts Were Candy…

We can program in such a way as to have our code make decisions for the end user. This logic can make for a better user experience and more intuitive program. One of the basic building blocks is an IF statement.

An IF statement decides whether something is true or false (boolean). Based upon the answer, appropriate code is run. Here is the basic syntax…

if(something is true){
     // Then do something 
}

That’s it. That is all there is to it. The key is to remember that IF statements always test boolean…true/false…yes/no. Here is another example…

if(10 === 10{
     alert('The tens are equal'); 
}

The aforementioned code would alert the end user that the tens were equal because it is true that 10 is equal to 10.

So that is one done with hard-coded values, but, typically, we use variables within our IF statements. Here is an example…

// Create variables to hold data 
var firstNum = 10; 
var secondNum = 10; 
// Compare the two variables 
if(firstNum === secondNum){
     alert('The variables are equal'); 
}

So, as you can see, we can compare two variables to test whether the comparison is true or false. Over the next several posts, I will add more nuances to the IF statements. Before long, you will see how useful they can be.

Happy Coding!

Clay Hess

HTML5 Session Storage

localstoragebefore

In yesterday’s post, I spoke of HTML5 Local Storage. Today, I would like to touch on HTML5 Session Storage. This will be a short post as session storage works a lot like local storage with one very large distinction…the persistence of the data.

With local storage, as you saw in my last post, the data survives to be accessed and utilized at a later browser session. Session storage is just the opposite. Any data that is stored in session storage “dies” after the browser session is complete. So how do we use Session Storage?

We will use the same code that we used for local storage…the HTML…

<!-- HTML to demonstrate HTML5 Local Storage --> 
<p>
     <!-- Button to click that will invoke our counter function. -->
     <button onclick="clickCounter()" type="button">Click me!</button>
 </p> 
<!-- Div that will hold the result of clicking the counter button. --> 
<div id="result"></div> 
<!-- Instructions. --> 
<p>Click the button to see the counter increase.</p> 
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>

Now for the JavaScript…

// Function demonstrate utilization of session storage 
    function clickCounter() {
         // Test whether browser has HTML5 storage capability
         if (typeof (Storage) !== "undefined") {
             // Test to see if there is an HTML5 session storage variable key called clickcount
             if (sessionStorage.clickcount) {
                 // Since the storage variable exists, let's add one to the value and store it back in the session storage variable
                 // We are running the variable through the number function to ensure it is an integer
                 sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
             } else {
                 // The clickcount session storage variable does not exist, so we create it.
                 // This should only run the first time we run the program.
                 sessionStorage.clickcount = 1;
             }
             // Grab our result div and display the results pulled from session storage
             document.getElementById("result").innerHTML = "You have clicked the button " + sessionStorage.clickcount + " time(s).";
         } else {
             // Display a message if the browser does not support HTML5 storage
             document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
         }
     }

Here is the beginning screen shot (it is the same as before)…

localstoragebefore

Here is a screen shot of the clicks in action…

localstorageafter

So, again, the big difference is that the counter data would “die” after closing the browser.

Happy Coding!

Clay Hess