In this first section, we will experiment with how variables are declared and assigned, as well as considering some of the different types of values in programming. You should have read the above slides to learn the relevant theory, but here is a recap of the programming basics you will need to complete the below tasks.
// You can declare a variable and assign it an initial value using the // following. Note that each statement must have a semicolon at the end. var x = 3; // You can assign a new value to any existing variable: x = 4; // console.log(...) allows you to output the 'console'. // In general, you can access the console by opening // your browser's debugging tools; for Chrome you can // press Ctrl + Shift + I. On this page any console outputs // will appear on the right. console.log(x); // You can get the program to prompt the user // for a value using 'prompt'. var y = prompt("Please enter a number"); console.log("The number entered was "+y);
Write code that carries out the following:
Number (there is no distinction between integers and reals),
We can convert between various primitive types. e.g.
Number("43") converts the string/text "43" into an actual number, so that we can subsequently do 'number' stuff to it, e.g. add other numbers to it.
Number(...). Guess what happens when the following code is run, and see if it matches your expectations. Note that
* in programming is multiplication.