Thursday, July 18, 2013

JavaScript: One Rep Max Calculator

Here's how to calculate your one rep max (ORM) with JavaScript in the weight room... If you have a computer near by.

I'm taking the Epley formula for calculating a one rep max which is this:

1RM = \frac{wr}{30} + w

So, it's weight times reps over thirty + weight. Here's how to translate it to Javascript.

First let's create a function:
 
function calculateORM(){};

I named this function calculateORM. I have empty space between the parentheses and the code brackets right now. Let's fill it in. Between the parentheses and code brackets I'll write:

function calculateORM() {
    (weight * reps) /30 + weight;
}

Which is the Epley formula for ORM translated into JavaScript. Notice I created two variables named weight and reps. I placed weight and reps as parameters in the parentheses. I'm not quite done yet. I'm going to put that formula into a variable like this:

function calculateORM() {
    var result = (weight * reps) /30 + weight;
}

I named it result. I tell the code to alert me of the ORM when I call it and I call the function:

function calculateORM(weight, reps) {
    var result = (weight * reps) /30 + weight;
    alert(result);
}
calculateORM(185,10)

I pass the numbers 185 and 10 in between the parentheses and JavaScript should give me an alert box that tells me what my ORM is.

Wednesday, July 17, 2013

Arrays

You can use arrays to store several elements in a variable. For example: var myArray = [1, 5, 2, 3];. This stores the numbers 1, 5, 2,3]. You can get any one part of an array because the elements in an array are indexed. The index is zero based, meaning the if I wanted to grab the second element of the array, I would write myArray[1]. This would grab the element 5.

Let's say I wanted to add all those elements together. I would write:



var myArray = [1,5,2,3];
var total = 0;
for ( var i = 0 ; i < myArray.length ; i++ ) {
    // add the current element to the total    
    total = total + myArray[i];
}
// after we're done with the loop
alert("The total is: " + total);

I set two variables: myArray = [1,5,2,3] and var total = 0.

I created a for loop with the variable i that starts at 0. The code block will be executed until the variable i is less than the length of the array (in this case, 4 times). The i++ part means that during each run of the loop, 1 will be added to the variable i.

The code block adds total to myArray[i]. During the first run of the loop, total will equal 0 and i will equal 0. myArray[0] will grab the first element of the myArray array. Thus total equals 1.

The code is executed again. Total will equal 1 because total was set to equal 1 during the first run of the loop. So, this time


total = total + myArray[i];


will add 1 to 5 and total will be set to 6. This code is executed until all elements of the array are added together.


Stong and weak-type languages

JavaScript is a weak-type language.

This means that you don't have to define what kind of variable you are working with. With a strong-type language, you would have to specify if you are setting a string, number, Boolean, etc. JavaScript is flexible in this way. This could lead to some unexpected results when writing code. For example, when you write console.log("5" + 5, JavaScript will convert the second 5 into a string and log 55 to the console. A different weak-type language may convert the string into a number.

Tuesday, July 16, 2013

Accessing the DOM

JavaScript can be used to get a handle on a particular part of an HTML document. Let's use this simple HTML as an example:

<html>
<head>
<title>
</title>
</head>
<body>
  <h1>My List</h1>
  <ul id="list"><li>Item 1</li><li>Item 2</li><li>Item 1</li></ul>
</body>
</html>

Here I have an unordered list with three list items. With JavaScript, I can use the the method document.getElementById() to grab a hold an element with a unique id. Notice that in the HTML, I gave the <ul> element an id of "list". We use ID's to access an element with CSS and we do it in a very similar way with JavaScript. If I write in my script:

 var list = document.getElementByID('list')

 I stored a variable called list that gives me access into the DOM. From here I can do things like:
  • determine the node type
  • change the inner HTML
  • and determine how many child nodes there are
Another method is to grab a hold of part of the DOM is document.getElementsByTagName(). If I write:

var listItems = document.getElementsById('li') 

This will find me all the nodes with the list item elements and store it as an array. If you wanted to access the second node for example, you would write

listItems[1]


And this will grab the second list item.

Sunday, July 14, 2013

Javascript Timer

Here's some of the Javascript I'm working on. This is an exercise from lynda.com. It's a timer that counts down from the amount of minutes you enter in a text box. This is mostly for my own education purposes.

I'll go through the code bit by bit to explain what it does. This is a bit hard to explain but I'll try. Here's all the Javascript code:

// JavaScript Document
var secondsRemaining;
var intervalHandle;
function resetPage() {
 document.getElementById("inputArea").style.display = "block";
};
function tick() {
 //grab the h1
 var timeDisplay = document.getElementById("time");
 //turn seconds into MM:SS
 var min = Math.floor(secondsRemaining / 60);
 var sec = secondsRemaining - (min * 60);
 // add a leading zero (as a string value) if seconds less than 10 
 if (sec < 10){
  sec = "0" + sec;
 };
 // concatenate with colon
 var message = min + ":" + sec;
 // now change the display
 timeDisplay.innerHTML = message;
 
 // stop if down to zero
 if (secondsRemaining === 0){
  alert("Done!");
  clearInterval(intervalHandle);
  resetPage(); 
 };
 // subtract from seconds remaining
 secondsRemaining--;
}
function startCountdown() {
 //get contents of the "minutes" text box
 var minutes = document.getElementById("minutes").value;
 //
 if (isNaN(minutes) ) {
  alert("Please enter a number!");
  return;
 }; 
 // how many seconds?
 secondsRemaining = minutes * 60;
 intervalHandle = setInterval(tick, 1000);
 //hide the form
 document.getElementById("inputArea").style.display = "none";
};
window.onload = function () {
 // create an input text box and give it an id of "minutes"
 var inputMinutes = document.createElement("input");
 inputMinutes.setAttribute("id","minutes");
 inputMinutes.setAttribute("type", "text");
 // create  a button
 var startButton = document.createElement("input");
 startButton.setAttribute("type", "button");
 startButton.setAttribute("value", "Start Countdown!");
 startButton.onclick = function(){
  startCountdown();
 };
 document.getElementById("inputArea").appendChild(inputMinutes);
 document.getElementById("inputArea").appendChild(startButton);
}; 

Here is the HTML:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>countdown</title>
<style type="text/css">
body{
 font-family:sans-serif;
 color:#333
}
#container {
 width:400px;
 margin:auto;
}
h1{ font-size: 5em;}
</style>
</head>

<body>

  <div id="container">
   <div id="inputArea">
    </div>
    <h1 id="time">0:00</h1>
   </div>
   <script src="script.js"></script>
</body>
</html>

When the window loads, the JavaScript will execute the code inside window.load:

window.onload = function () {
 // create an input text box and give it an id of "minutes"
 var inputMinutes = document.createElement("input");
 inputMinutes.setAttribute("id","minutes");
 inputMinutes.setAttribute("type", "text");
 // create  a button
 var startButton = document.createElement("input");
 startButton.setAttribute("type", "button");
 startButton.setAttribute("value", "Start Countdown!");
 startButton.onclick = function(){
  startCountdown();
 };
 document.getElementById("inputArea").appendChild(inputMinutes);
 document.getElementById("inputArea").appendChild(startButton);
}; 


In here I create two input elements. One text input and one button input. Then I grab a hold of a div element in the HTML with the inputArea id (look in the HTML), and insert the elements inside.

After a user clicks on the button we created, the code will execute the startCountdown() function:


function startCountdown() {
 //get contents of the "minutes" text box
 var minutes = document.getElementById("minutes").value;
 //
 if (isNaN(minutes) ) {
  alert("Please enter a number!");
  return;
 }; 
 // how many seconds?
 secondsRemaining = minutes * 60;
 intervalHandle = setInterval(tick, 1000);
 //hide the form
 document.getElementById("inputArea").style.display = "none";
};

I grab a hold of the text box I created. There is an if statement which says: if the value of what is entered in the text in not a number, alert the user to enter a number. If it is a number: I multiply the value entered by 60 (60 seconds in a minute) and name it "secondsRemaining". I create a variable called "intervalHandle" and set it to a setInterval method which will execute the "tick" function ever 1s (1000 milliseconds). The last bit hides the form by grabbing the div element, and creating CSS to set the display to "none".

Now to the "tick" function:

function tick() {
 //grab the h1
 var timeDisplay = document.getElementById("time");
 //turn seconds into MM:SS
 var min = Math.floor(secondsRemaining / 60);
 var sec = secondsRemaining - (min * 60);
 // add a leading zero (as a string value) if seconds less than 10 
 if (sec < 10){
  sec = "0" + sec;
 };
 // concatenate with colon
 var message = min + ":" + sec;
 // now change the display
 timeDisplay.innerHTML = message;
 
 // stop if down to zero
 if (secondsRemaining === 0){
  alert("Done!");
  clearInterval(intervalHandle);
  resetPage(); 
 };
 // subtract from seconds remaining
 secondsRemaining--;
}

Here I grab the h1 element (which contains 00:00). I turn the minutes to the seconds remaining and divide it by 60. I want to use Math.floor so that it doesn't round up or down. Second will be set to the seconds remaining minus min time 60. The if statement makes sure that there is a zero after the colon if there is less than 10 seconds. You wouldn't want the timer to look like this:

00:9

When there is 9 seconds left.

I set timeDisplay to equal min + ":" + sec. The text in the timer will equal whatever min and sec are, with the colon. The if statement tells the page what to do when the timer reaches zero. It will make the text box and button appear again. It will also clear the setInterval method.

The secondsRemaining part is what tells the seconds to subtract 1 from seconds, every time the tick code block is run.

OK, this was a very poor explanation of how the code works, but like I said, this is for my own educational purposes. I promise I will be more clear in the future!

Thursday, July 11, 2013


I'm no expert on getting a job in the field. I've only had one tech writing job, and I've only had it for 7 or 8 months at that. Despite this, I'm going to share with you the little nuggets that I've learned in case you are yourself a young college grad who doesn't know what the hell to do with the fancy liberal arts degree hanging on your wall.

#1

Learn a technical skill. Preferably with computers. Despite some people say, you should have some facility with technnology. The interwebs give you no excuse not to learn a thing or two about technology. You will have to sometimes learn things on the fly in this field. Learning technical skills will make it easier to learn what you need to know to get the job done.

#2

Write. Make a portfolio of speculative work. If you have no experience, this is the only way to get experience. I got my job from making a spec portfolio that included a very sparse vacuum cleaner, users manual. My fiance thought I was crazy. It was very good practice in the end. You'll also see if you actually like the work or not.

#3

Learn what structured authoring is. This guy give a pretty good introduction to structured authoring.

#4

Learn how to use graphics in your work. You will probably need to know some basic things about Photoshop, Illustrator, or any other graphic editing tool. Adobe gives month long free trials for their products. I hear there's some good free software, too. Although, I haven't tried any of the free programs myself.