Making Stuff Happen with Javascript (Case)

This is a case study for use in a class like Software Development.

Please note: This case builds on a prior case called ‘Making HTML Manageable with CSS‘ and assumes familiarity with its contents.

Prototyping for Usability

Frangelico DeWitt and his team have just started developing a new suite of software for their employer HVAC in a Hurry (a heating and air conditioning service business). Their current focus is on a web application that helps field technicians find availability and pricing for replacement parts.

HTML-Prototype

Frangelico has been prototyping the front end with HTML and CSS, making pretty good progress. What he’s realized is that he wants to prototype a few interactions so they can move forward user testing their refinements. He could script this out with static HTML, but both for the sake of creating more (potentially) reusable assets and making the test infrastructure a little more robust, he’s decided to implement the interactions with Javascript.

The good news is that he has an a working on JS fiddle and a set of user stories about what he wants to test (see Appendix A).

As a next step, Frangelico has decided he needs to make the prototype interactive so he can test this user story:
‘I don’t know the part number and I want to try to identify it online so I can move the job forward.’
and these test cases:
Make sure it’s possible to search by make/model of units
Make sure it’s possible to search by type

Making Stuff Happen

He’s made a start with this JS Fiddle: Making Stuff Happen with Javascript.

Looking at the sample code in the JS Fiddle above, can you toggle (show/hide) the #search-controls div with JS? Specifically, how about having a click on the div with ID ‘toggle-search’ show/hide the div with ID ‘search-controls’?

How about a somewhat more involved challenge?! Right now, the code allows the user to filter by manufacturer and model. How would you extend it to also filter by part type?

What else do you think is worth trying? Try it out!

Exhibit A: User Stories

These stories are the primary focal point for the team: they describe both what the team has learned about the user and what they want to do for them. As you get into the details, it’s key not to lose sight of your design intent and what you’ve decided is important to the user.

Epic User Story

‘As Ted the HVAC technician, I want to identify a part that needs replacing so I can decide my next steps.’

Storyboard

Based on their observations in the field, Frangelico’s team thought through the user experience of the epic with the following storyboard: hvac-epic-story

Child Stories

User Story

Test Cases

‘I know the part number and I want to find it on the system so I can figure out next steps on the repair.’ Make sure it’s possible to search by part number.
Make sure descriptive info. appears as the search narrows (photo?) to help avoid error.
‘I don’t know the part number and I want to try to identify it online so I can move the job forward.’ Make sure it’s possible to search by make/model of units
Make sure it’s possible to search by type
‘I don’t know the part number and I can’t determine it and I want help so I can move the job forward.’ Make sure an estimate of the turnaround time for an expert to review is available
‘I want to see the cost of the part and time to receive it so I decide on next steps and get agreement from the customer.’ Make sure it’s possible to dispatch a request by email to the customer in case they order their own parts and/or carry their own inventory of spares.
NOTE: How would the customer respond so we can help structure the next steps as we would otherwise?
Make sure it’s possible to indicate priority
Make sure cost associated with priority delivery are available
‘I want to order the part so that we can move forward with the repair.’

Exhibit B: Working with the Sample Code on JS Fiddle

Notes on Javascript:

  1. You can place Javascript directly in the Javascript pane without the <script> tags.
  2. In the HTML panel, you don’t need any of the exterior HTML tags, <body> on outward.

Exhibit C: Coding with Javascript

Javascript (JS) is a programming language, extremely popular for coding related to user interfaces. It was the first programming language supported natively by web browsers, and remains the only such language. What this means is that you can write JS and a user can then simply run it in their browser, where other languages required various other intermediate steps and supporting infrastructure to work. JS is most widely used for facilitating dynamic interactions with HTML for user interfaces. In this exhibit, you’ll get an introduction to both (assuming you’re starting from zero programming experience) and in the Resources section below there’s more detailed material.

Javascript- What Is

While machines are getting smarter and smarter, you’ll probably be surprised at how literal and specific you have to be when programming. A single semicolon in the wrong place can break your whole program. That may sound daunting, but all it really means is that you have to be very step-wise and test-driven when you program.

What does Javascript look like? For starters, the snippet below has two lines of Javascript (JS) code. If you click on the Result tab, you’ll see two alerts pop up into your browser window (unless your pop-up blocker blocks them):

Notice that each full line/statement ends with a semicolon.

Logging to the Console

Since all of the work we’re doing is in the browser, that’s a good place to look at what your code is doing so you can expand and debug it in a test-driven way. Unless you have a strong preference and prior experience doing otherwise, I recommend using Google’s Chrome browser, and the rest of this material assumes that.

The ‘console’ is basically a place you can see statements that Javascript sends there, which, as I mentioned, is a very common thing for JS coders to do. Recently, JSFiddle made a change where they implement the console inside the Fiddle itself. I prefer the native Chrome Console, and that’s what the balance of this case (and related cases) shows. However, this is not currently the default in JSFiddle. Once you open the sample code in JSFiddle, you’ll need to:
1: Click ‘Settings’ in the upper right
2: Disable the option ‘Console in the editor’

js-fiddle-ugh.001From there, to see the console  in Chrome just right click within the browser and click ‘Inspect’. Now in the top of menu of the frame that appears, click ‘Console’. You’re there. For more, see this tutorial: The Console in Chrome.

The JS Fiddle below has the two statements you saw before, but now they’re sent to the console:

If you have the console open and you click Result, you’ll see the results of those two statements. Pro tip: You’ll probably be seeing other output from content on this page and maybe your own Chrome plug-in’s. If you use the filter control in type ‘index’, you’ll see just the JS Fiddle stuff.

If you want to know why something is or is not happening, the console is your friend. You’ll see extensive use of it in the JS Fiddle for the case itself (Making Stuff Happen with Javascript).

Note: You will not see the console in JS Fiddle itself. The ‘Result’ screen shows what a user will see.

The If … Else Statement

Another thing you’ll notice in the sample code is if … else statements. Here’s a JS Fiddle with an example:

For more on that, check out this quick note: The If Else Statement in Javascript.

Comments and Commenting Out Code

You’ll see comments and you’ll probably want to use them to make notes to yourself and temporarily disable code as you’re trying things. For more on that, see this quick note: Comments in Javsascript.

Variables

Variables are a place you can store a value to use it later. If that sounds abstract, trust me, you’ll be doing it all the time once you get coding. In this snippet, we declare a variable ‘x’, change its value, and log that value to the console.

Quick note on variables vs. comparisons: In JS, ‘==’ is a comparison operator, meaning it asks ‘are these two things equal?’. The operators ‘!=’ does the opposite: checks if they’re not equal.

Interacting with View Elements (HTML & CSS)

JS has broad and deep support for interacting with end user ‘views’, particularly those implemented in HTML & CSS. Here’s an example, which is a simplified view of what you’ll see in the case:

observer-pattern.001

Below is the JS Fiddle with the function body for filterParts() expanded. Note: my recommendation is just to have a quick look at this and then open the actual JS Fiddle and try making changes to it live so you can actively see how it works.

Supplemental Resources (Only if You’re Stuck)

  1. Written JS Tutorial on W3Schools: Intro to JS
    While it’s probably more breadth than you need to get started, this tutorial does a nice job stepping through the fundamentals.
  2. Just for Backup: Introduction to Javascript on Codecademy
    If you just feel overwhelmed by programming and Javascript, you can ease into the material very gently by starting here. I noticed that it didn’t allow me to skip items within a lesson but you can skip forward to different lessons.
  3. Just for Reference: Symbol Reference for Javascript