Download presentation
Presentation is loading. Please wait.
Published byLionel Sharp Modified over 9 years ago
1
Introduction to Programming (in JavaScript) in 10 minutes …hopefully Else
2
Variables In algebra x = 5 x + x = 10 In JavaScript var x = 5; x + x == 10; Can also store other types of data, not just numbers Arrays, strings, integers, floats, classes, and functions Demo We assign values to variables with = We assert something is equal with ==
3
Data Types Integer 1, 2, 3, 4, 5… var x = 5 +, -, /, *,, == String ‘dog’, ‘cat’, ‘bird’… var x = ‘dog’ or var x = “dog” +, substring, == Array – list of items var animals = [‘dog’, ‘cat’, ‘bird’] animals[0] == ‘dog’
4
Functions In algebra F(x) = x + 5 F(5) = 10 In JavaScript var F = function(x) { return x + 5; } F(5) == 10 var F_b = function(x, y) { return x + y; } F_b(5, 3) == 8 Functions can take any type of input a variable can take – any kind of JavaScript data type Demo Notice: Every line of JavaScript must end with a ;
5
Scope Global variables and local variables Global variables are anything you define by itself in a.js file or inside tags Local variables are defined inside a function and isolated to that function var x = 10; function valOf(y) { return y; } function myFunc(x) { x = x + 1; return x; } valOf(x); myFunc(15); valOf(x); myfunc(15) will return 16, not 11 And valOf(x) always equals 10
6
Scope …continued var z = 100; function A(x){ return x + z; } A(10); A(10) return 110, because the Variable z is not defined in the function
7
Boolean True or false 5 < 3 returns false 5 > 3 returns true 3 == 3 returns true ‘dog’ == ‘dog’ returns true Demo
8
Conditionals “If this is true do this, else do this…” function evenOrOdd(mynum) { var remainder = mynum % 2; if(remainder == 0) { return ‘even’; } else { return ‘false’; } function relationship(x, y) { if(x > y) { return ‘larger’; } else if(x < y) { return ‘smaller’; } else { return ‘equal’; }
9
Loops While and For Loops function countToTen(x) { while(x <= 10 ) { alert(x); x = x + 1; } function alertArray(a) { for(var i = 0; i < a.length; a++){ alert(a[i]); } Demo
10
End Introduction to Programming Phew!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.