Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active March 6, 2024 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisterBrash/81399e934111744477515a4e92008f5f to your computer and use it in GitHub Desktop.
Save MisterBrash/81399e934111744477515a4e92008f5f to your computer and use it in GitHub Desktop.
ICS3U Lesson 1.3 - Mathematics

1.3 - Mathematics

ICS3U - Mr. Brash

Lesson - Mathematics in Code

The symbols we use to do math are called operators.
The list of operators is available here. The list of assignment operators is available here.

// Add, subtract, etc...
let x = 1;
x = x + 5;
x = 7 - 2;
x = x * 3;
x = 24 / 8;

// Exponent
let four_squared = 4**2;
let six_cubed = 6**3;

// Math Shortcuts
x = x + 3;
x += 3;   // Add 3 to x's current value

x = x - 2;
x -= 2;   // Reduce x's value by 2

x *= 4;   // Multiply 4 into the value of x
x /= 2;   // Divide x by 2 and store it back in x

// Incrementation (increase or decrease the variable by 1)
x++;
x--;

/* Anything in "quotes" is text (called a String)
 * We can combine (concatenate) strings */
let first = "Mr.";
let last = "Brash";
let myName = first + " " + last;      // The simple way

// It is NOT possible to subtract strings
myName = myName - "B";  // Not possible

The Math Object

JavaScript has a special Math object for doing complicated stuff.

  • Math.random() // Get a random decimal number bettwen 0 and 1
  • Math.round() // Round to the nearest whole value
  • Math.sqrt() // Take the square root of the number

Examples:

// Calculate the area of a circle with radius of 3
let r = 3;
let area_circle = Math.PI * r**2;
// Calculate pythagorean theorem
let a = 8;
let b = 4;
let c = Math.sqrt(a**2 + b**2);
console.log("The hypotenuse of a triangle with sides", a, "and", b, "is", c)

1.3 - Mathematics Task

We are going to ask the computer to convert temperatures.

The equation to convert Fahrenheit to Celsius is:

C = (F - 32) * 5/9

In the above equation, we know the Fahrenheit value F and use it to create and store the new Celsius value into C.

The equation to convert in the opposite direction is:

F = (C * 9/5) + 32

All the code below should go in a new file 1.3 - Mathematics.js

Part 1: Convert Celsius to Farenheit

  1. Declare a variable and call it celsius, setting celsius to 100.
  2. Declare another variable and call it fahrenheit - setting this variable to 0.
  3. Let's add 2 to celsius, mostly to practice that skill.
  4. Using the correct equation (above) and the value of the variable celsius, convert that temperature to degrees Fahrenheit and store that new value in your variable fahrenheit. This will overwrite the value of 0 you set in step 2.
  5. Output the value of fahrenheit to the console. Just the number.

Part 2: Convert Farenheit to Celsius This task will reuse the variables from Part 1 - no new let statements.

  1. Now set the variable fahrenheit to 77
  2. Using the correct equation, convert from this Fahrenheit value to Celsius, and store the answer in the celsius variable.
  3. Output the current value of celsius to the console. Just the number.

Part 3: Now for some text: Printing just the numbers is boring. Let's try that output again (on a new line) but with some pizzazz!

  • Using the variables farenheit and celsius create an output that looks like this: "77 degrees Fahrenheit is {value} degrees celsius." (where {value} is whatever the answer was to the conversion done in Part 2).

Last Part:

  1. Slope of a line is defined as m = (y2 - y1)/(x2 - x1). Define a variable m and compute the slope from point A(-4, 5) to point B(6, 0), and store it into m.
  2. Print the value of m to the console.

"What do I do when I'm done?"

Congratulations! No need to humble brag. Find something to learn. For example:

  • Try this task in another programming language
  • Research HTML and make a simple webpage
  • Lookup coding tutorials online



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment