Lab work 6 - ENTERING THE SECOND REALM - JavaScript in Plain Language (2015)

JavaScript in Plain Language (2015)

PART II: ENTERING THE SECOND REALM

2.4 Lab work 6

Ready for more lab work?

Create a program to convert Fahrenheit to Celsius

Operator precedence determines the order in which operators are evaluated. JavaScript precedence is close to math precedence:

· Parentheses are evaluated before multiplication or division,
and multiplication or division are evaluated before subtraction or addition.
All being equal, operands are evaluated from left to right and from inside of parentheses out.
...nothing to memorize, just practice a lot.

The following is the formula to convert Fahrenheit to Celsius:

Deduct 32 from the variable F,
then multiply the result by 5,
then divide the new result by 9.

We need to include the subtraction within parentheses so that it is done first. Then write the multiplication next so that it is done before the final division which should be placed on its right side. The formula looks like this:

(f - 32) * 5 / 9

That was the hard part. Now let’s code.

1- Create a function named f2c that takes 1 input parameter called f.

2- Inside of the function send the result of the formula to the screen.
In other words use console.log() to print to screen. This is because we only want to display it, not to reuse it. Notice how the input parameter’s name f coincides with the variable given to our formula. That was done on purposes so the JavaScript interpreter knows where to plug the input number we are going to give it when we call the function.

3- Call the function and pass in an argument of 212 degrees Fahrenheit. (write just the number by itself). Your displayed answer should be 100.

See my own solution here (first script): f2c | bit.ly/1Dp2SKk

Create a program to convert Celsius to Fahrenheit

The formula to covert Celsius to Fahrenheit is

Multiply c by 9,
then divide the result by 5,
then add 32 to the new result.

There is no need to insert parentheses, just make sure the multiplication comes first:

c * 9 / 5 + 32

1- Create a function named c2f that takes 1 parameter called c.

2- Inside of the function send the result of the formula to the screen.
In other words use console.log() to print to screen.

3- Call the function and pass in an argument of 100 degrees Celsius (just the number).
Your answer should be 212.

See my own solution here (second script): c2f | bit.ly/1Dp2SKk

Why are we using console.log() instead of return?

As you may remember, console.log() was designed for quick testing outputs. At this time we are not concerned with returning a value for further processing. When we need to reuse a value we will use return and assign the value to a variable.

However, please read the next topic for information about the return mechanism.

In JavaScript a function always returns something

In case you are wondering whether the function call gets cleared out of memory when we don’t explicitly use return, the answer is yes, in JavaScript it does.

In JavaScript every function returns automatically when the code interpreter reaches the closing brace, if not before.

When that happens, JavaScript will return the value of undefined if a return statement was not manually written.

In production you will not visually see this automatic return but on the test Console you will see undefined in the end if you didn’t ask for a specific return. If your Console is still open, look at the last function call. You will see the term undefined below the number 212.

This automatic return assures proper memory management. Memory management is actually done by the browser, not by JavaScript. Different browsers have different garbage collection mechanisms.

A program to convert Fahrenheit to/from Celsius

In this project we are going to introduce a function with two input parameters. The first input parameter will take a numeric value, and the second input parameter will serve as a condition to toggle between Celsius and Fahrenheit.

How will we toggle between Celsius and Fahrenheit?

We can use an if(), else if(), else statement:

a) If, the user enters an f for Fahrenheit, the if() output will convert Fahrenheit to Celsius.

b) Else if, the user enters a c, the number gets converted from Celsius to Fahrenheit.

c) Else, the script will output something like
"Sorry, that conversion is not supported".

When we create a function with multiple input parameters like for example...

function test(x,y) { return x - y; }

...we need to call the function and pass in the data arguments in the proper sequence.

The value to be assigned to x (which is the leftmost input parameter) should be written in the function call as the first data argument. On the following function call, 10 is grabbed by parameter x and 3 is grabbed by parameter y:

test(10,3);

Notice how we separate them by commas. Had I entered 3 first, 3 would become x and the output result would be incorrect.

Let’s start:

1- Create a function named conversion that takes two input parameters named num (reminds me of number), and degrees. Notice the comma separating the input parameters:

function conversion(num,degrees) { }

2- Inside of the function create your first if() statement that says
if(degrees === "f") {
and then it console.logs the formula to convert Fahrenheit to Celsius.
The formula is (f - 32) * 5 / 9 However, please read the note below:

NOTE: you must replace the f in the formula with our new generic input parameter num.

Bonus: add a + "c" just before the closing parentheses of your console.log(). This is so that the output will look like 100c. Do the same on your next step for "f".

3- Next create the else if() statement that says
else if(degrees === "c") {
and then it console.logs the formula to convert Celsius to Fahrenheit.
The formula is c * 9 / 5 + 32

NOTE: you must replace the c in the formula with our new generic input parameter num.

4- Finally, create a catch all else statement that console.logs
"Sorry, that conversion is not supported".

5- Close the function with a curly brace }.

6- Paste your code onto the Console and test it with the following function calls:
conversion(212, "f"); <-- should result in 100c
conversion(100, "c"); <-- should result in 212c
conversion(300, "b"); <-- Should result in “Sorry, that conversion is not supported”.
Remember, the character input is a string and it should be in quotes.

(See my own solution here (third script): conversion | bit.ly/1Dp2SKk

Congratulations!

Don’t stop here!! Please take a look at the next page for more practice ideas.

You need to start creating other programs of your own.

Below please find some suggestions to create other conversions:

a) Perimeter
Suggested function design: calcPerimeter(length,width) {
Formula: length + length + width + width

b) kilometers into miles
Suggested function design: kiloMiles(km) {
Formula: km * 0.6214

c) feet to meters
Suggested function design: feetM(ft) {
Formula: ft * 0.3048.

Go for it : spend a few hours creating new stuff.

If you would like to save your scripts, paste them onto a plain text editor such as Windows NOTEPAD and save them with the extension of .txt which represents the plain text format.

We will cover other ways of storing executable JavaScript programs later.

END OF LAB