More JavaScript Functions

This week we had some more practice creating functions that pass data between each other. The assignment programs were a little more difficult than others we’ve done up to now, but the principles of accepting and storing user input, passing arguments to another function which uses them as parameters for a specific task, and then returning values back to the original function seem like such important concepts to master. I’m glad to have had the chance to work on this and add it to my tool belt, but I feel like there is still so much to learn

Checking for Input

Our assignments were to create an investment calculator and a loan calculator. After I had created the main functionality of the investment calculator, I wanted to add a way to test for empty user input boxes and output an error message to the user if necessary. I wasn’t sure how to do that, but with some help from Sheldon & Matthew in the peer review I learned about the isNaN() function, which tests the value in the parentheses and returns a Boolean response.

So I used an if statement with some logic to test each of the required input fields. Then I learned a little bit about how to manipulate the CSS styling of an element using JavaScript, and I changed a paragraph element containing my error message from display:none to display:block if isNaN() returned true on any of the tests. This worked much better than I expected, and I was pleased with the result. I used the same method of testing input fields in the loan calculator, which was an even more complex program than the investment calculator.

Descriptive Names

Another tip I got from the peer review was to use variable names that are descriptive and useful. In both calculators, I broke up the more complex formulas into smaller bite-sized chunks and stored those chunks in their own variable before putting them together for the final calculation. When I did that, I used variable names that were not descriptive on their own, such as ar, or ppy, or some single letter abbreviation.

While I did use comments liberally, Sheldon suggested I use descriptive names on my variables and parameters, even if they are longer. Doing so, he explained, helps in reading the code and immediately knowing what’s going on, especially if you’re reading somebody else’s code. Most of my classmates are good at doing that, and looking at Andrea’s code, for example, I noticed that his variable names were very descriptive and I could quickly see the purpose of each variable and parameter. So that is a practice I completely agree with, and something I will implement better as I go forward.

Investment Calculator Loan Calculator