COSma Coding

COSma Coding

COSma Learning

What is this site?

This is site was written by Grant Sander, and is an extension of cosmalearning.com. It was developed to support learners in exploring both math *and* programming. The site consists of various tutorials, examples, and exercises, ranging from basic programming concepts to some sophisticated mathematical ideas. Througout the site you will see code snippets that look like this:

```
print("I'm an editor!");
print("You can edit me, and run the code.");
```

Click on the code snippet to pull open the code in the editor on the right-side of the screen. You can edit the code in the code-editor, and then press the "Run" button to run your code. The output of your code will be shown below the code editor. *You can also press Ctrl + Enter (or Cmd + Enter on Mac) to run your code.*

The code editors are running JavaScript code. JavaScript is a programming language that nearly all websites use -- it is the code that runs in your web browser. So, anytime you are using an interactive website, chances are there is some JavaScript running behind the scenes. In this site you will also have access to some functionality that is beyond basic JavaScript to make the exercises more interesting.

You will see the `print()`

command *very* often throughout the site. The `print()`

command will print an output to the output section of the editor. As an example, if you want to know what 4 + 6.5 is, you could run `print(4 + 6.5);`

.

What is Coding/Programming?

*Coding*, or *programming*, is the process of writing computer programs. Computer programs are basically just commands telling a computer what to do. Let's look at a simple example of a program.

`print("I'm a program!");`

In the code editor above, there is a single line: `print("I'm a program!");`

. This line of code is just telling the computer, "print the statement 'I'm a program!' to the output". We are just telling the computer what to do in a language that it can understand. When looking at a program, the computer will generally work its way down the program, executing one line at a time. For example, consider the following program:

```
print("Hello, world!");
print("It's a great day to be alive!");
print("It's a great day to learn about coding!");
```

In the above program, there are now three lines of code. The computer just works its way down those three lines of code, executing each line of code when it gets to it. Try editing some of those lines, or adding your own! See how it changes the output to the right.

There are many different types of programming, and *many* different programming "languages". We won't deal with the differences between these languages and types of languages - instead we will use a particular language, *JavaScript*, to help us in our exploration of general coding ideas.

Adding Comments to Code

In JavaScript we can add comments to code using `//`

notation. So, `// THIS IS A COMMENT`

will be interpreted as a comment, and the computer will not do anything with it. It's just so that we can add comments to our code so we can remember what we did. You will see these comments regularly throughout the code in this site.

Math Capabilities

JavaScript has some built-in math operators, such as addition (+), subtraction (-), multiplication (*), and division (/). You can also use parentheses for grouping, like in math. The editor below shows some these operators in use.

```
print(3 + 5);
print(10/2);
print(5 + (3*2));
print(6/(2+1));
```

When doing math in JavaScript (and other programming languages), it is important to write math statements *in a way that the computer can understand*. As an example, the computer won't understand `3(5)`

. You will need to enter `3*(5)`

instead.

To make this site more fun, mathematically, I have added a handful of extra math capabilities. For example, I have added some useful mathematical functions and constants - including the constant \(e\) and \(\pi\), as well as trigonometric and logarithmic functions. The editor below shows some of these math functions. It's okay if you don't know what all of them are, just be aware that you have some additional math functions available.

```
// Constants
print( e );
print( pi );
// Trig functions
print( sin(0.5) );
print( cos(0.5) );
print( tan(0.5) );
print( arcsin(0.5) );
// 3^2 is written as pow(3, 2)
print( pow(3, 2) );
// log_4(16) written as log(16, 4)
print( log(16, 4) );
// Natural log
print( ln(e) );
```

The table below is a list of the additional math functions and what they do.

Function/Constant | Description | Example |
---|---|---|

`e` | The constant \(e\) | `2*e` for \( 2\cdot e\) |

`pi` | The constant \(\pi\) | `pi/2` for \(\frac{\pi}{2}\) |

`pow(a, b)` | Base to a power | `pow(3, 7)` for \(3^7\) |

`abs()` | Absolute value | `abs(-5)` for \(|-5|\) |

`sqrt()` | The square root function | `sqrt(16)` for \(\sqrt{16}\) |

`nroot(x, n)` | n^{th} root | `nroot(27, 3)` gives \(\sqrt[3]{27}\) |

`sin()` | The sine function | `sin(pi/2)` for \(\sin\left(\frac{\pi}{2}\right)\) |

`cos()` | The cosine function | `cos(pi/2)` for \(\cos\left(\frac{\pi}{2}\right)\) |

`tan()` | The tangent function | `tan(pi/4)` for \(\tan\left(\frac{\pi}{4}\right)\) |

`asin(), arcsin()` | The inverse sine function | `asin(0.5)` for \(\sin^{-1}(0.5)\) |

`acos(), arccos()` | The inverse cosine function | `acos(0.5)` for \(\cos^{-1}(0.5)\) |

`atan(), arctan()` | The inverse tangent function | `atan(0.5)` for \(\tan^{-1}(0.5)\) |

`exp()` | The exponential function (base e) | `exp(2)` gives \(\exp(2) = e^2\) |

`log(x, b)` | The log function, base b | `log(100)` for \(\log(100)\), `log(16,4)` for \(\log_4(16)\) |

`ln()` | The natural log function | `ln(e)` gives \(\ln(e)\) |

`gcd(a, b)` | The greatest common denominator of a and b | `gcd(25, 15)` returns 5 |

`lcm(a, b)` | The least common multiple of a and b | `lcm(7, 5)` returns 35. |

`ceil()` | The ceiling function | `ceil(2.3)` for \(\lceil 2.3 \rceil\) |

`floor()` | The floor function | `floor(2.3)` for \(\lfloor 2.3 \rfloor\) |