Wifi network: CrossCamp.us Members
Wifi password: innovate
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Access the slides to follow along here:
Functions are reusable pieces of code.
First, declare the function.
<script>
function turtleFact() {
document.write('A turtle\'s lower shell is called a plastron.');
}
</script>
Then, use it as many times as you want!
<script>
turtleFact();
</script>
A variable is a place to store values
$
, or _
.$
, or _
.To declare (create) a variable, just type the word var
and the variable name.
<script>
var numberOfKittens;
</script>
It is a good idea to give your variable a starting value. This is called initializing the variable.
<script>
var numberOfKittens = 5;
</script>
Once you have created a variable, you can use it in your code. Just type the name of the variable.
<script>
var numberOfKittens = 5;
document.write(numberOfKittens);
</script>
The scope of a variable is how long the computer will remember it.
A variable declared outside a function has a global scope and can be accessed anywhere, even in a function.
var awesomeGroup = 'Girl Develop It'; //Global scope
function whatIsAwesome() {
document.write(awesomeGroup + ' is pretty awesome.'); //Will work
}
whatIsAwesome();
A variable declared within a function has a local scope and can only be accessed within that function.
function whatIsAwesome() {
var awesomeGroup = 'Girl Develop It'; //Local scope
document.write('I made a variable called awesomeGroup with a value of ' + awesomeGroup); //Will work
}
whatIsAwesome();
document.write(awesomeGroup + ' is pretty awesome.'); //Won't work
if
statementUse if
to decide which lines of code to execute, based on a condition.
if (condition) {
// statements to execute
}
var bananas = 5;
if (bananas > 0) {
document.write('You have some bananas!');
}
if/else
statementUse else to provide an alternate set of instructions.
var age = 28;
if (age >= 16) {
document.write('Yay, you can drive!');
} else {
document.write('Sorry, but you have ' + (16 - age) + ' years until you can drive.');
}
If you have multiple conditions, you can use else if.
var age = 20;
if (age >= 35) {
document.write('You can vote AND hold any place in government!');
} else if (age >= 25) {
document.write('You can vote AND run for the Senate!');
} else if (age >= 18) {
document.write('You can vote!');
} else {
document.write('You can\'t vote, but you can still write your representatives.');
}