//PROCESS: Basic math

## True fact: Computers are good at math!

Regular
math
Javascript
math
On the keyboard Try it out in an alert!
3+2 `3 + 2` + is shift=
3−2 `3 - 2` -
3×2 `3 * 2` * is shift8
3•2 `3 * 2` * is shift8
3⁄2 `3 / 2` /
3÷2 `3 / 2` /
`Math.pow( 3, 2 )`
`Math.pow( 2, 3 )`
3√2 `3 * Math.sqrt( 2 )`
∛2 `Math.pow( 2, (1 / 3) )`
sin(3°) `Math.sin( 3 * Math.PI / 180 )`
cos(3°) `Math.cos( 3 * Math.PI / 180 )`
tan(3°) `Math.tan( 3 * Math.PI / 180)`
π `Math.PI`
`parseInt( "35 degrees" )`
`parseFloat( "3.14159cm is a pithy measurement" )`

## Cleaning your data with `parseInt()`

Often the data that you read in your program is dirty — particularly when you are converting between datatypes, like converting strings to numbers.

This is where `parseInt()` comes in handy. `parseInt()` looks at a string, and extracts the first number that it sees.

For example, `parseInt( "1855 Trollope Street" )` will return the number `1855`. It is a real problem-solver!

If you need to get a floating decimal, use `parseFloat()` instead. For example, `parseFloat( "100.3km/h" )` will return the number `100.3`.

## Common mistakes

• Forgetting to capitalize `Math.``math.PI` does not work!
• Not enough brackets — Sometimes order of operations behaves strangely.
Add brackets around mathematical subclauses when in doubt:
`4 + 3 * 12 + 4` becomes `4 + ( 3 * 12 ) + 4`

## Code example

...is made with the following code:

``````<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>drapak.ca - Area of a circle</title>
<meta name="description" content="This script calculates the area of a circle">
<meta name="author" content="Dave Drapak">
<!--
Change log:
27. Sept 2015   - created
3. March 2017   - reworked to move script to the end
9. October 2018 - updated id styles and added parseInt  - Drapak
11. Oct 2018    - fixed poor style in input line        - Drapak
16. Oct 2018    - clarified example code                - Drapak
-->

<body>
<h1>Calculate the area of a circle</h1>

<p>
What is radius of the circle?

<input id="inputId" type="text" value="5">

<button id="buttonId">
Calculate the area!
</button>
</p>

<p>The area is: <strong id="outputId"></strong></p>

<script>
// read the input and output the
var mainProcedure = function () {
console.log( 'in mainProcedure...' );

//INPUT: get the radius from the input line
var inputNumber     = document.getElementById( 'inputId' ).value;

//PROCESS: clean up the input from the line by extracting the integer part
var parsedNumber    = parseInt( inputNumber );

var answer          = Math.PI * ( Math.pow( parsedNumber, 2) );