slides.com/jkohlin/js-3/live
Till next week: Read chapter 4
var answer;
do {
kiss();
} while( confirm("Do you want another one?") )
The bad boy of loops.
Do first - ask later.
outerLoop:
while( confirm("continue?") ) {
var answer = prompt("your name:");
var loops = 0;
while(loops < 5){
loops += 1;
if (answer == "Joe") {
break outerLoop;
}
}
}
Add them just above a loop or, actually, any code block.
Then by adding break yourLabelName; you will jump to the end of that code block
one:
{
var rnd = Math.random();
if (rnd > 0.5) {
break one;
}
console.log("rnd is less than 0.5");
}
console.log(rnd)
reusable save-for-later-code-blocks
Encapsulated scripts that won't run until you tell it to run, or until the user tells it to run.
Functions can be versatile and dynamic by using parameters. That way, one function can do different things.
One of the most important part of a modern programming language.
var score = 0;
var gameOn = true;
while (gameOn == true) {
if ( prompt("blue pill or red pill?") == "blue pill" ) {
break;
} else {
score += 1;
}
if ( prompt("good or evil?") == "evil" ) {
break;
} else {
score += 1;
}
if ( prompt("What is the answer?") == "42" ) {
break;
} else {
score += 1;
}
gameOn = confirm("play again?")
}
var score = 0;
var gameOn = true;
function quizQuestion (q, a) {
if ( prompt( q ) == a ) {
break;
} else {
score += 1;
}
}
while (gameOn == true) {
quizQuestion( "blue pill or red pill?", "blue pill" );
quizQuestion( "good or evil?", "evil" );
quizQuestion( "What is the answer?", "42" );
gameOn = confirm( "play again?" )
}
<body>
<button onclick="pling()">Pling</button>
</body>
function pling() {
audioElement = document.querySelector("audio");
audioElement.play();
}
uint64_t s[2];
uint64_t next(void) {
uint64_t s1 = s[0];
uint64_t s0 = s[1];
uint64_t result = s0 + s1;
s[0] = s0;
s1 ^= s1 << 23; // a
s[1] = s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26); // b, c
return result;
}
Math.random(); // A lot easier than the code below
function ( ) {
}
The function keyword begins a function definition
function bark( ) {
}
Next we give the function a name, like bark
function bark(name, weight) {
}
We call these the parameters
and, separate, them, by, commas
function bark(name, weight) {
if (weight > 20) {
alert(name + " says WOOF WOOF");
} else {
alert(name + " says woof"):
}
}
We call this the body of the function
see page 90
The first argument you pass to a function will be the value of the first parameter.
See page 83-86
Let's just, once and for all
get expressions
var c = 4;
function foo(num) {
return num/2;
}
var a = foo( c * 3 ) ;
var c = 4;
function foo(num) {
return num/2;
alert('boo hoo, I will not popup');
}
var a = foo( c * 3 ) ;
also quits the function.
return works like break in a loop. It will return a value and then stop reading the rest of the code in the function
😲😱
see page 98-108
var gName = "Jane";
function printer() {
var name = "Tarzan";
verb = " loves "
console.log(name + verb + gName);
}
console.log(gName);
see page 98-108
var gName = "Jane";
function printer() {
var name = "Tarzan";
verb = " loves "
console.log(name + verb + gName);
}
console.log(gName);
LOCAL
GLOBAL
If you forget the var keyword inside functions,
the variable will be declared in the global scope