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.

115 lines
3.1 KiB
JavaScript

console.log("Array Traversals")
let studentNumberA = '2020-1923'
let studentNumberB = '2020-1924'
let studentNumberC = '2020-1925'
let studentNumberE = '2020-1926'
let studentNumberD = '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 for ([]) also known as Array Literals
// - Commonly used to store numerous amounts of data to manipulate in order to perform a number of tasks.
// Majority of methods are used to manipulate information stored within the same object
// - Syntax
// let/const arrayName = [elementA, elementB, elementC ]
let grades = [98.5, 94.3, 89.2, 90.1]
let computerBrands = ['Acer', 'Asus', 'Lenovo', 'Neo', 'RedFox', 'Gateway', 'Toshiba', 'Fujitsu'];
console.log(grades);
console.log(computerBrands);
// console.log(mixedArr);
// Alternative way to write arrays
let myTasks = [
'drink html',
'eat javascript',
'inhale css',
'bake sass'
];
// Creating 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);
let fullname = 'Jamie Noble';
console.log(fullname.length);
// length property on strings show the number of characters in a string.
// Spaces are counted as characters in strings.
// length property can also set the total number of items in an array. meaning we can actually delete the last item in the arraw or shorten the array by simply updating the length property of an array.
myTasks.length = myTasks.length-1;
console.log(myTasks.length);
console.log(myTasks);
// To delete a specific item in an array we can employ array methods ( which will be shown in the next session ) or an algoritm (set of code to process tasks. )
// Another example using decrementation
cities.length--;
console.log(cities);
fullname.length = fullname.length-1;
console.log(fullname.length);
fullname.length--;
console.log(fullname);
let theBeatles = ["John", "Paul", "Ringo", "George"];
theBeatles.length++;
console.log(theBeatles);
// theBeatles.length = theBeatles.length+2;
// console.log(theBeatles);
// [SECTION] Reading from arrays [ Index Numbers ]
// 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"];
console.log(lakersLegends[1]);
// Access the second item in the array
console.log(lakersLegends[3]);
// Access the fourth item in the array
let currentLaker = lakersLegends[2];
console.log(currentLaker);