You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.1 KiB
JavaScript

console.log("Hello world!");
// An Array in programming is simply a list of data. Let's write the example earlier.
let studentNumberA = "2020-1923";
let studentNumberB = "2020-1924";
let studentNumberC = "2020-1925";
let studentNumberD = "2020-1926";
let studentNumberE = "2020-1927";
// Now, with an array, we can simply write the code above like this:
let studentNumbers = ["2020-1923", "2020-1924", "2020-1925", "2020-1926", "2020-1927"];
// [SECTION] Arrays
/*
- Arrays are used to store multiple related values in a single variable.
- They are declared using square brackets ([]) also known as "Array Literals"
-Syntax:
let/const arrayName = [elementA, elementB, elementC, ...]
*/
// Common examples of arrays
let grades = [98.5, 94.3, 89.2, 90.1];
let computerBrands = ["Acer", "Asus", "Lenovo", "Neo", "Redfox", "Gateway", "Toshiba", "Fujitsu"];
// Possible use of an array but is not recommended
let mixedArr = [12, "Asus", null, undefined, {}];
console.log(grades);
console.log(computerBrands);
console.log(mixedArr);
// Alternative way to write an array
let myTasks = [
'drink html',
'eat javascript',
'inhale css',
'bake react'
]
// Create an array with values from variables:
let city1 = "Tokyo";
let city2 = "Manila";
let city3 = "Jakarta";
let cities = [city1, city2, city3];
console.log(myTasks);
console.log(cities);
// [SECTION] length Property
// The .length property allows us to get and set the total number of items in an array.
console.log(myTasks.length);
console.log(cities.length);
let blankArr = [];
console.log(blankArr.length);
// .length property used on strings
let fullName = "Jamie Noble";
console.log(fullName.length);
// .length property can also set the total number of items in an array.
myTasks.length = myTasks.length - 1;
console.log(myTasks.length);
console.log(myTasks);
// Another example using decrementation:
cities.length--;
console.log(cities);
// We can't do the same on strings
fullName.length = fullName.length - 1;
console.log(fullName.length);
// increase/lengthen the array
let theBeatles = ["John", "Paul", "Ringo", "George"];
theBeatles.length++;
console.log(theBeatles);
// [SECTION] Reading from Arrays
/*
- Accessing array elements is one of the more common tasks that we do with an array
- This can be done through the use of array indexes
- Syntax:
arrayName[index]
*/
console.log(grades[0]);
console.log(computerBrands[3]);
// Accessing an array element that does not exist will return "undefined"
console.log(grades[20]);
let lakersLegends = ["Kobe", "Shaq", "LeBron", "Magic", "Kareem"];
// MINI ACTIVITY
// Access the second item in the array:
console.log(lakersLegends[1]);
// Access the fourth item in the array:
console.log(lakersLegends[3]);
// Storing array item in a variable
let currentLaker = lakersLegends[2];
console.log(currentLaker);
// Reassigning array values using item's index
console.log("Array before reassignment:");
console.log(lakersLegends);
// reassign the 3rd item to "Paul Gasol":
lakersLegends[2] = "Paul Gasol";
console.log('Array after reassignment:');
console.log(lakersLegends);
// Accessing the last element of an array
let bullsLegends = ["Jordan", "Pippen", "Rodman", "Rose", "Kukoc"]; // length = 5
let lastElementIndex = bullsLegends.length - 2;
console.log(bullsLegends[lastElementIndex]);
[]
// You can also add it directly:
console.log(bullsLegends[bullsLegends.length - 1]);
//Adding Items into the Array
let newArr = [];
console.log(newArr[0]);
newArr[0] = "Cloud Strife";
console.log(newArr);
console.log(newArr[1]);
newArr[1] = "Tifa Lockhart";
console.log(newArr);
// Adding an item at the end of the array
newArr[newArr.length] = "Barrett Wallace";
console.log(newArr);
// Looping over an Array
// You can use a for loop to iterate over all items in an array
for(let index = 0; index < newArr.length; index++){
// You can use the loop counter (index) as index to be able to show each array items in a console log.
console.log(newArr[index]);
}
// Given an array of numbers, you can also show if the following items in the array are divisible by 5 or not.
let numArr = [5, 12, 30, 46, 40];
for(let index = 0; index < numArr.length; index++){
if(numArr[index] % 5 === 0){
console.log(numArr[index] + " is divisible by 5");
} else {
console.log(numArr[index] + " is not divisible by 5");
}
}
// [SECTION] Multidimensional Arrays
/*
- Multidimensional arrays are useful for storing complex data structures
*/
let chessBoard = [
['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1'],
['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2'],
['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3'],
['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4'],
['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5'],
['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6'],
['a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7'],
['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
];
console.log(chessBoard);
// Accessing elements of a multidimensional array
console.log(chessBoard[1][4]);
console.log("Pawn moves to: " + chessBoard[1][5]);