Location where you declare a variable will affect where it can be used.
A variable declared inside a function can only be used in that function
A variable declared outside of a function can be used anywhere within the script.
Location where you declare a variable will affect where it can be used.
A variable declared inside a function can only be used in that function
function printArea(width,height)
{
var area = width * height;
console.log("Area is: " + area);
}
Location where you declare a variable will affect where it can be used.
A variable declared inside a function can only be used in that function
function getArea(width,height)
{
var area = width * height;
return area;
}
var size = getArea(10,3);
console.log("Area is: " + size);
A variable declared outside of a function can be used anywhere in within the script.
Location where you declare a variable will affect where it can be used.
A variable declared inside a function can only be used in that function
function getArea(width,height)
{
var area = width * height;
return area;
}
var size = getArea(10,3);
console.log("Area is: " + size);
console.log("Area is: " + area);
A variable declared outside of a function can be used anywhere in within the script.
Full Script
getArea()
var size
var area
Location where you declare a variable will affect where it can be used.
A variable declared inside a function can only be used in that function
function getArea(width,height)
{
var area = width * height;
return area;
}
var size = getArea(10,3);
console.log("Area is: " + size);
console.log("Area is: " + area);
A variable declared outside of a function can be used anywhere in within the script.
Stores a List of Values
var colour1 = "red";
var colour2 = "green";
var colour3 = "blue";
Stores a List of Values
var colours = ['red','green','blue'];
Each Item is automatically given an Index (Index starts at 0)
'red' | 'green' | 'blue' |
---|
Index used to access specific items
var colours = ['red','green','blue'];
var thirdItem = colours[2]; //'blue'
'red' | 'green' | 'blue' |
---|
Index also used to change items in array
var colours = ['red','green','blue'];
var thirdItem = colours[2]; //'blue'
colours[1] = 'purple'
'red' | 'purple' | 'blue' |
---|
New data can be added to the array
var colours = ['red','green','blue'];
colours[3] = 'pink';
'red' | 'purple' | 'blue' |
---|
'pink' |
---|
Getting the length of the array
var colours = ['red','green','blue'];
var myColoursLength = colours.length;//3
In Chrome JavaScript Console do the following (Ctrl+Shift+J to open console)
Arrays have some built in functions which we can use.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//to add a new fruit we can do this...
fruits[4] = "Pear";
fruits[5] = "Grapes";
//However we need to always keep track of the size to do this
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//a better way to add is to use push
fruits.push("Pear");
fruits.push("Grapes");
//We can just keep adding without keeping track of the length
Adding Values to an Array (Method 1 - Index)
Adding Values to an Array (Method 2 - Push)
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//remove the last element "Mango"
var lastFruit = fruits.pop();
//lastFruit is now "Mango"
Use Pop to remove the last element item in an array
'Banana' | 'Orange' | 'Apple' |
---|
'Mango' |
---|
fruits =
lastFruit =
'Mango' |
---|
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//add "Pineapple" to the start of the array
fruits.unshift("Pineapple");
//["Pineapple","Banana", "Orange", "Apple", "Mango"]
Use Unshift to add to the front/start of an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//remove the first element "Banana"
var firstFruit = fruits.shift();
Use Shift to remove from the front/start of an array
var fruits = ["Pineapple","Banana", "Orange", "Apple", "Mango"];
//find the index of "Orange" in the array
var indexOfOrange = fruits.indexOf("Orange"); //2
//find the index of "Pineapple" in the array
var indexOfPineapple = fruits.indexOf("Pineapple"); //0
//find the index item that doesnt exist returns a -1
var indexOfNotThere = fruits.indexOf("Kiwi"); //-1
Find the index of a specific item in an array
var fruits = ["Pineapple","Banana", "Orange", "Apple", "Mango"];
//copy the 3rd and 4th fruits
//slice words by accepting the start and end index of the subset
var subset = fruits.slice(2,4);// ["Orange","Apple"]
Copy subset of an array
2 Dimensional Arrays
var groups = [[1,2,3],[4,5,6],[7,8,9]];
var thirdGroup = groups[2]; //[7,8,9]
var thirdGroup_secondItem = thirdGroup[1];//8
[1,2,3] | [4,5,6] | [7,8,9] |
---|
var thirdGroup_secondItem = groups[2][1];//8
var cars = ["Ford","Nissan","Honda","VW"];
console.log(cars[cars.length]);
var cars = [
["Ford","Cadillac","Chrysler"],
["Audi","BMW","VW"],
["Toyota","Honda","Nissan"],
];
console.log(cars[2][0]);
var cars = [
["Ford","Cadillac","Chrysler"],
["Audi","BMW","VW"],
["Toyota","Honda","Nissan"],
];
console.log(cars[0][1]);
var cars = [
["Ford","Cadillac","Chrysler"],
["Audi","BMW","VW"],
["Toyota","Honda","Nissan"],
];
console.log(cars[1]);
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//print all values in the array
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
console.log(fruits[3]);
//not good...what if array had 100 elements..or 1000
var fruits = ["Banana", "Orange", "Apple", "Mango"];
for(var i=0; i < fruits.length; i++)
{
console.log(fruits[i]);
}
Loop over an array to to something to/with all items in an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function consolePrint(color){
console.log(color);
}
//pass function name as argument
fruits.forEach(consolePrint);
arrayVariable.forEach(someFunction);
var x = [1,2,3,4,5,6,7,8,9,10];
function myFunction(number)
{
if(number % 2 === 0)
{
console.log(number * number);
}
}
x.forEach(myFunction);
printReverse()
Write a function printReverse() which takes an array as argument and prints out the elements in the array in reverse order.
getProduct()
Write a function getProduct() which takes an array as argument and returns a number which is the product of all number is the array.
Asssume that an array of numbers will be input
e.g:
[1,3,5,4] = 1 x 3 x 5 x 4 = 60
getMax()
Write a function getMax() which takes an array as argument and returns a number which is the maximum of all number is the array.
Asssume that an array of numbers will be input
e.g:
[1,3,5,4] = Max is 5
isPassed()
Write a function isPassed() which takes a single numeric arguement and returns true if the number is greater than 40 and false otherwise
factorial()
N! : factorial of N is multiplication of all positive numbers less than or equal to N
4! = 4 x 3 x 2 x 1
6! = 6 x 5 x 4 x 3 x 2 x 1
isValidEmail()
Hint: use to check each character to find "@" and "."
Additional Checks could look at position of "@" and "."
isValidPassword()
A valid password is defined as:
isValidAge()
A valid age is defined as:
Hint: parseInt() to convert from string to number (see here)