Loading
Johan Kohlin
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
Chapter 1 (loops) +
Chapter 2
var foo = 42;
/* is the same as:
* var foo;
* foo = 42;
*/
// Do not write:
var baz;
var baz = prompt('gimme a number');
// only declare once
What does it mean?
Can we use it alone?
When should we NOT use it?
var height; // find an empty memory cell and name it height
height = 187; // now put 187 in the memory cell called height
"RAM memory"
3.14159
42
0.01
-12
10000
"anything within double quotes"
'or "single" quotes'
"add a line break \n Like this"
// either
true
// or..
false
typeof 1337 // number
typeof "42" // string
typeof true // boolean
/* Put parentheses
* around expressions
*/
typeof ("20"+1) // string
typeof ("2" == 2) // boolean
Test the type of a value or expression
The world famous recipe for a random number 0-n
var max = 10;
var random = Math.floor(Math.random() * max + 1);
Only for numbers from 0 to max
The famous recipe for any random number interval
var min = -15; // ANY number you want
var max = 10; // a higher number than min.
var random = Math.floor(Math.random() * (max-min+1) + min);
AKA logical operators - outcome combo is either true or false.
var x = 12;
var y = "12";
( x === y || x == y )
// true because ONE comparison MUST be true
(x == y && typeof x == typeof y)
// false, because BOTH comparison MUST be true
&& = AND (2 ampersands) || = OR (2 pipe characters)
...before assigned to a variable or passed to a function
var b = 20;
var c = 3;
var a = b * 2 + Math.round( c / 2 ) ;
ALL EXPRESSIONS MUST FIRST RESOLVE TO A VALUE before assigned
-start from the left (if possible)
var b = 20;
var c = 3;
var a = b * 2 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 20 * 2 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 20 * 2 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( c / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( 3 / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( 3 / 2 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( 1.5 ) ;
var b = 20;
var c = 3;
var a = 40 + Math.round( 1.5 ) ;
var b = 20;
var c = 3;
var a = 40 + 2 ;
var b = 20;
var c = 3;
var a = 40 + 2 ;
var b = 20;
var c = 3;
var a = 42 ;
var b = 20;
var c = 3;
var a = 42 ;
Using the browser's built in dialog windows
alert("This is an alert!")
prompt("What's your name")
returns the value as a string
confirm("Do you want to continue?")
returns true or false
// alert does not return anything
alert('just a popup with a message');
//prompt returns a string value
prompt('A popup with a text field');
// confirm returns a boolean, true or false
confirm('a popup with a message and \ncancel or OK button');
\n in a string will cause a line break
The browser's built in dialog windows
alert("Good morning! \nToday is the first day\nof the rest of your life")
This also works for confirm() and prompt() of course
Using the browser's built in developer console
var msg = "log your variables to console.log() to see their value here";
console.log(msg);
console.error("If your game isn't working you can see why in the console");
By writing directly to the HTML
<body>
<h1>Welcome!</h1>
<script>
document.write('<p>To this dynamic web page</p>');
</script>
</body>
<body>
<h1>Welcome!</h1>
<p>To this dynamic web page</p>
</body>
write on the HTML page where the script tag is
using document.write(htmlCode)
By manipulating the HTML
var h1Pointer = document.querySelector('h1');
h1Pointer.innerHTML = 'Hello, <b>world!</b>';
<body>
<h1> <- Everything between these are innerHTML -> </h1>
</body>
<body>
<h1>Hello, <b>world!</b></h1>
</body>
but more on this later
Fencing one or many statements
var guess = 0 var answer { guess++ answer = prompt('Guess a number') }
They don't do much on their own ...
if (destination == "Råslätt") {
"Take the left path"
} else {
"Take the right path"
}
if (true == true) {
/* Code in this block
* Will only run if
* the test is true. */
}
A test (boolean expression)
after the script has run
var points = 0
var skill = 3
if (skill > 3) {
points = points + 1
}
console.log("Your current score is " + points)
Can only follow an if-statement
var points = 0
var extra = confirm('want a point?')
if ( extra ) {
points = points + 1 // only if extra is true
} else {
points = points - 1 // only if extra is false
}
Otherwise
Can only follow an if-statement
if ( extra ) {
points = points + 1 // only if extra is true
} else if (points == 0){
alert ("Can't get any lower")
/* only if extra is false
* and points is 0 */
} else {
points = points -1
}
OK, well, what about...
Otherwise
while (true == true) {
/* Code in this block
* Will run again and again
* as long as the test is true. */
}
A test (boolean expression)
var points = 0;
if ( points < 5 ) {
points = points + 1;
console.log( "you have " + points );
}
var points = 0;
while ( points < 5 ) {
points = points + 1;
console.log( "you have " + points );
}
var score = 0; while ( confirm("your current score is " + score) ) { score = score +1; }
while (boolean expression is true) { we are stuck in here until: a) the boolean expression is false, or b) we encounter the keyword break }
while (true) { guess = prompt('guess a number'); if ( guess == 7 ) { break; } }
do {
kiss();
} while( confirm("Want another one?") )
The self-confident loop –Do first, ask later.
all caps need to be in the same direction
0 1 2 3 4 5 6 7 8 9
You can say either
"number 0: switch", "number 2: switch"
or
"number 3 to 5, switch"
How many times must you yell out
var current = 0
var students = "011100"
while ( current < students.length ) {
if (students.charAt(current) != "O") {
switch() // made up function...
}
current++ // short for current = current +1
}
If we were to program this instead, we could use loops
var legs = 4 | Declare legs and set the value to 4 |
while(legs > 0) { | Loop while legs are more than 0 |
legs = legs -1; | Decrease the value of legs by 1 |
} | End loop |
if (legs == 0) { | If legs is 0 then |
legs = 4; | set it back to 4 |
} | end if |
There is no pseudocode standard, So use any words you like.
The company you end up working for might have a style guide though.
Flowcharts though, have a standard.
a) A night at Akademin
b) How to pass an exam
c) Order a foot long at Subway
set money to 900
set sober as true
set beers to 0
while money is more than 0
increase beers by 1
decrease money by 50
dance()
if sober then
orderShot()
else
dance('on table')
end if
end while
sleep()