By Now, You Should Be Able To… Discuss the Pillars of Info Assurance (CIANA) and
apply them to concepts, tools, scenarios, etc. Convert between binary, hex, decimal, and
ASCII. Discuss the primary functions/services that an
Operating System provides. Discuss processes, user accounts, permissions. Comfortably navigate the file/folder structure of
Windows & Unix via the shell (command line). Utilize multiple commands (win & unix) via the
shell: dir, ls, copy, move, mkdir, rmdir, whoami, hostname, ipconfig, ping, etc. Be proactive w/ EI, MGSP, etc.!
Programs
Files containing instructions for the CPU Can write a program in this
manner Almost no one does
Programs are written in a programming language Set of commands that are
converted to CPU instructions Commands are written ahead of
time and then compiled or interpreted
We will be using JavaScript as our programming language
31 c0 31 db 31 d2 31 c9 b0 04 b3 01 59 b2 05 cd 80 31 c0 b0 01 31 db cd 80 e8 e2 ff ff ff 68 65 6c 6c 6fWho wants to write a program like
this?
Believe it or not, this is MUCH easier.
Programming Part I 4
Compiled vs. Interpreted Languages
Compiled languages Commands are converted to CPU instructions
before the program executes Machine code stored in program file
Interpreted languages Commands are evaluated and converted to
CPU instructions at run time Requires an interpreter program Programming language code stored in program
file JavaScript is an interpreted language
Programming Part I 5
Javascript
A scripting language for browsers. Every browser includes a Javascript
interpreter Javascript programs are not meant to run
directly on a physical machine, but instead "run on the browser“…which itself is a Program running on the physical machine. This kind of "program running within a
program", is actually extremely common, is often referred to as a script.
Programming Part I 6
JS Statements & Expressions
A program is a collection of statements and expressions Translated into CPU instructions
Statement is a command to perform an action document.write("Hello World"); Action can be to evaluate an expression
Expression is a statement that evaluates to a value x = x + 1;
Statements and expressions are terminated with the ‘;’ character. Not required in JavaScript, but good practice
Programming Part I 7
Literals vs. Variables
A literal is a constant value 2016 “a string”
A variable is a symbolic name for a known or unknown value Consider it as a “temporary container” for a
value …and the value stored in that container can
change When the variable’s name is used, it
evaluates to the value in the container
Programming Part I 8
Operators
Program element that is applied to one or more operands in an expression or statement
Types Arithmetic Assignment Comparison Boolean
We will cover comparison and Boolean operators later
Programming Part I 9
JS Arithmetic OperatorsOperat
orDescription Exampl
eResult (x)
+ Addition x = y + 2;
7
- Subtraction x = y – 2;
3
* Multiplication x = y * 2;
10
/ Division x = y / 2;
2.5
% Modulus (remainder) x = y % 2;
1
Note: y = 5 for all examples
• Order of Operations maintained as before• Student Exercise: Enter an expression to sum the integers from 1 to 10
• Pull up: <a href="http://rona.cs.usna.edu/~si110/resources/interpreterBare.html">interpreter</a>
Programming Part I 10
Math functions: Beyond simple arithmetic ops, JS has established math functions: math functions:, Math.sin( ), Math.cos( ), Math.log( ), Math.exp( ),
Math.sqrt( ), Math.floor( ), etc. Ex: Math.sqrt(a): Yields square root of variable a complete reference of Math functions and constants
Student exercise: Write a single expression that gives exactly the answer to the following problem: calculate the hypotenuse of a right triangle with base sides 68 and 23.5.
NOTE: unfortunately, there is no exponentiation! Student exercise: Write a single expression that gives exactly
the answer to the following problem: Your restaurant bill is $73.50. What would you tip if you wanted to give at least an 18% tip, but you wanted the tip to be a whole number of dollars?
JS Arithmetic & other Operators
Programming Part I 11
Now…Using Variables
var x;
x = 5;
x = 2000;
x = x + 2;
5
20005
2000 + 2 = 2002
Programming Part I 12
Variables: Javascript lets you define variables, which are simply names that point to values.
Once a name is declared as a variable, that name evaluates to the most recent value it points to.
Usually, we could declare and assign simultaneously in the same line,
e.g.: var x = Math.sqrt(17);
In Javascript: x = x + 1 …makes sense, because it assigns x a new value: its original value + 1
What you want to do How it is scripted
Declare name x as a variable:
var x;
“assign” name x a value: x =0 or x=Math.sqrt(17)
evaluate name x to get its value:
x
Now…Using Variables
Programming Part I 13
Variable Types Identifies the type of data a value (either
literal or variable) represents JavaScript types
Number String Boolean Function Object
We will focus on Numbers and Strings Booleans to a lesser extent
Programming Part I 14
Variable Changes
1. var x = 32. var z = 23. x = x + z4. x = x +
z*z
What is value of x What is value of z
Programming Part I 15
Numbers
Literal type used to represent a numeric value Whole numbers (3, 1, 4) Decimal numbers (3.14)
Various operations can be performed Basic arithmetic
Usual arithmetic operation ordering applies Math functions
Math.sin(x), Math.exp(x), Math.sqrt(x), etc. JavaScript provides defined mathematical constants
Math.PI, Math.E, etc. Operations, functions, and constants can be
combined Math.sqrt(Math.PI * 3 * 3);
Programming Part I 16
Strings
Every value in Javascript has a “type”, numbers are one of them, “strings” are another
String is a sequence of characters Enclosed within single or double quotes ( ‘ or “ ) Zero-length string (i.e., no characters) is called an
“empty string” Examples
‘foo’ “bar” ‘3’ “3.14” “Hello, world!” “” this is the empty string (zero length)
Programming Part I 17
Escape Characters
A sequence used to insert special characters into a string literal which would otherwise cause a “syntax error” Backslash (\) followed by sequence that
denotes the character Often used to print characters that
Are difficult (or impossible) to type Would be interpreted as part of the program’s
syntax
Programming Part I 18
Escaping Examples
\’ or \” – insert a single or double quote Enter this in the line interpreter: “I said “hello”.”
What happened? Correct: “I said \”hello\”.”
Enter this in the line interpreter: ‘This can’t work!’ Correct: ‘This can\’t work!’
\n – insert a new line “This string \nhas two lines” prints as:
This stringhas two lines
\\ - insert a backslash “The directory is c:\navy” prints as:
The directory is c:avy
Correct: “The directory is c:\\navy”
Programming Part I 19
Working with Strings
+ operator Used to concatenate (join) strings together
var message = “Hello, ” + “world!”; yields the new string “Hello, world!”
JavaScript automatically interprets operators based on data types 7 + 5 yields 12 “7” + “5” yields “75”
Programming Part I 20
String Subscripts A String is a collection of zero or more characters If you want to pick out a character from a string at a
certain position, you can do it by "subscripting". Subscripting allows the programmer to reference a specific character in a String
Positions of characters within a string by is called an index. The trick is that index position 0 corresponds to the first character
in a string, which may be counter-intuitive. The syntax is string[index position].
So, if var x = "hard", the expression x[0] evaluates to the letter h, the expressions x[1] evaluates to a, etc.
The referenced character is at the position (index) denoted by the subscript
If var foo = “Hello, world!”; what would foo[3] would evaluate to?
Programming Part I 21
String Functions JavaScript Strings have some built-in functions
var_name.length Returns the number of characters in a string “Hello”.length what does this yield?
var_name.charAt(x) Returns the character at position x “Hello”.charAt(4) what does this yield?
var_name.charCodeAt(x) Returns the ASCII value of the character at position x “Hello”.charCodeAt(4) what does this yield?
String.fromCharCode(x) Returns a string consisting of the character represented by
the ASCII value x String.fromCharCode(67) what does this yield?
Programming Part I 22
“Type” Determination
typeof operator Special operator used to determine the type of
a value: commonly a “string” or a “number” Examples
typeof(1989) → What “type” does this yield? typeof(“2016”) → What “type” does this yield
Can call typeof for an expression typeof(2000 + 2) → Number typeof(“200” + “2”) → String
Programming Part II 23
Type Conversion
JavaScript [tries to] automatically convert numbers to strings and strings to numbers when it deems necessary Implicit conversion
Can also perform explicit conversion Number(x)
Attempts to convert the argument to a Number String(x)
Attempts to convert the argument to a String
Programming Part II 24
Type Examples
typeof(2016 + 2); Number → 2018
typeof(“201” + ”6”); String → “2016”
typeof(1008 * “2”); Number → 2016
typeof(201 + “6”); String → “2016”
typeof(2000 + Number(“16”)); Number → 2016
typeof(“20” + String(16)); String → “2016”
QUESTIONS?
Operating Systems Part II 25
Top Related