JavaScript Functions

JavaScript Functions

This week we practiced creating functions that pass information between each other. I began to realize how handy this can be in keeping a program’s code organized in nicely defined chunks (modules). A function can be created to accept parameters from another function, do a very specific job with those parameters, and then even return the result back to the function that called it. This is great for tasks that need to occur many times within a program: you can create a function to do the task (with different parameters each time), and then call it as often as you need it.

Our first assignment this week was to create a program that calculated wind chill. It had to consist of two functions, one that accepted user input for temperature and wind speed and then passed those arguments to another function that used them as arguments to calculate the wind chill. Here is my wind chill program.

I was able to get my program working without too much trouble, other than some frustration with a single syntax error that took me a while to find, even with the Chrome Developer Tools debugger. After finding and fixing that error, the code ran fine. However Sheldon, a fellow student, mentioned that when he ran my program the ° (degree) symbol didn’t display properly. He suggested I use the HTML entity instead of the actual symbol itself in the code, so I made that change.

Stacie, another student, posted her code with a question about how to round off her answer. I had the same question when I wrote my program and I found the answer with the .toFixed() method, which founds the answer to the number of decimals specified in parenthesis. I told Stacie about how I had used it, and she successfully implemented it in her own code.

The concept of interacting functions that pass data among themselves is great, and I’m looking forward to writing more powerful programs using this new skill.

Leave a Reply

Your email address will not be published. Required fields are marked *