Thursday, May 6, 2021

Angry Birds (Part 5)

 Angry Birds

(Part 5)


*Please use desktop mode here in order to find link of codes*
Codes:

Array:

We stored data most commonly inside variables. In Javascript, a variable holds different types of data: 
  • String: These are sequence of characters stored inside quotes 
  • Number: Any mathematical number 
  • Boolean values: true and false values
There are also two special types of data : 
  • null: it means nothing or empty 
  • undefined: it means that no value has been assigned to a variable.
A single variable javascript can hold any of these types of data. Different data can also be reassigned to a single variable. Examples:

//Examples on the different types of data in javascript

//String
var string = "Hello Friends"
console.log(string);

//Number
var num = 1256;
console.log(num);

//Boolean
var bool = true;
console.log(bool);

//Undefined
var object;
console.log(object);

//Reassigning the same undefined object to null
//Null
object = null;
console.log(object);


The disadvantage of storing data in a variable is, we can store only one value at a time. Thus, it is too cumbersome to create a variable for each value. It would also make our code unreadable.

To avoid this problem, Javascript and most other languages have different data structures to hold multiple values. One popular data structure to hold multiple values is called an "array". An array is created inside square brackets and can store a list of same or different types of data separated by a comma. Example:

//Example of arrays

//an array holding same data type
var arr1 = [1,2,5,8];
console.log(arr1);

//an array holding different data type
var arr2 = ["Hello",1284,true]
console.log(arr2);


An array stores a list of arrays! Example:

//an array storing a list of arrays
var arr3 = [[5,9],[2,5],[8,11]];
console.log(arr3);

Each value in the array is indexed by a number. The first value has an index of '0', the second value has an index of '1' and so on. (Counting always starts from 0 in most computer languages). If we want to access the first element of our arr3 array, we can access it by using arr3[0]

//an array storing a list of arrays
var arr3 = [[5,9],[2,5],[8,11]];
console.log(arr3);

//access first element of array
console.log(arr3[0]);

To access the first element inside the first element of the array. We did this by adding a sub-index like this arr3[0][0]. The second element of the first element in the array can be accessed with arr3[0][1].

//an array storing a list of arrays
var arr3 = [[5,9],[2,5],[8,11]];
console.log(arr3);

//access first element of array
console.log(arr3[0]);

//access the second element of the first element of array
console.log(arr3[0][1]);


A new value can be pushed inside an array by using array.push().

//an array storing a list of arrays
var arr3 = [[5,9],[2,5],[8,11]];
console.log(arr3);

//Adding a string value in in an array 
arr3.push("Sweets");
console.log(arr3);


Similarly, the last values can be pooped out of the arrays using array.pop()

//an array storing a list of arrays
var arr3 = [[5,9],[2,5],[8,11]];
console.log(arr3);

//Adding a string value in in an array 
arr3.push("Sweets");
console.log(arr3);

//Removing last value in an array
arr3.pop();
console.log(arr3)

ASCII Codes:



Video:





Previous Post
Next Post

post written by:

0 Comments: