0% found this document useful (0 votes)
22 views22 pages

GIAIC - 04-Arrays and Strings

Uploaded by

hasssannann234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views22 pages

GIAIC - 04-Arrays and Strings

Uploaded by

hasssannann234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to Arrays

YOUR FIRST DATA STRUCTURE IN PROGRAMMING


What is an Array

 An ordered collection of values: An array is like a


train where each car holds a value.
 Same data type: Usually, all the values in an
array should be of the same type (numbers,
strings, etc.).
 Indexed access: We can reach individual elements
using their position (index), starting from 0.
Declaring Arrays

let arrayNameWithoutType=[]
let arrayName: type[];
let numbers = []; // 'numbers' is implicitly any[]
// Array of numbers:
let numbers: number[] = [1, 2, 3, 4];
// Array of strings:
let names: string[] = ["Alice", "Bob", "Charlie"];
// Using the Array type
let mixedArray: Array<number | string> = [1, "hello", 2];
Accessing and Modifying Array Elements using
[]
// Accessing elements by index:
let firstNumber = numbers[0]; // firstNumber will be 1

// Modifying elements by index:


names[1] = "Bobby"; // Changes "Bob" to "Bobby"

// **Caution:** Out-of-bounds access throws an error.


// numbers[10] = 100; // This would cause an error
Accessing and Modifying Elements using []

 Direct access with square brackets: We use the variable name


followed by square brackets [] containing the desired element's index.
 Modification: Similar syntax is used to change an element's value at a
specific index.
Common Array Methods

 Adding elements:
 .push(value) – Appends an element to the array's end.
 .unshift(value) – Inserts an element at the array's beginning.
 Removing elements:
 .pop() – Removes and returns the last element from the array.
 .shift() – Removes and returns the first element from the array.
 Iterating through arrays:
 for...of loop – A loop that iterates over the values in the array.
 .forEach() method – A higher-order function that executes a provided function once
for each array element.
How Push and Unshift works in TypeScript

let fruits: string[] = ['Apple', 'Orange', 'Banana'];


fruits.push('Mango'); // Adds "Mango" to the end
fruits.unshift('Strawberry'); // Adds "Strawberry" to the beginning
How Pop and Shift in works in TypeScript

let lastFruit = fruits.pop(); // Removes and returns the last element


console.log(lastFruit); // Outputs: "Mango“

let firstFruit = fruits.shift(); // Removes and returns the first element


console.log(firstFruit); // Outputs: "Strawberry"
Iterating(Reading) the Elements of an Array In
TypeScript using For Loop
 Using For Loop to read the elements of an Array

for (let i = 0; i < fruits.length; i++) {


console.log(fruits[i]);
}
Iterating(Reading) the Elements of an Array In
TypeScript using ForEach Function
 Using For Loop to read the elements of an Array

fruits.forEach((fruit) => {
console.log(fruit);
});
Iterating(Reading) the Elements of an Array In
TypeScript using For…Of loop
 Using For…Of Loop to read the elements of an Array
// Looping through the 'fruits' array using a for...of //
loop:
for (let fruit of fruits) {
console.log(fruit); // Outputs: Apple, Orange,
// Banana
(each on a new line)
Advanced Array Methods

 .find(callback):
 This method searches the array for the first element that meets a specific condition
defined in a callback function. It returns the matching element or undefined if no
match is found.
 .filter(callback):
 This method creates a new array containing only the elements that pass a test
defined in a callback function.
 .map(callback):
 This method creates a new array by applying a callback function to each element of
the original array. The callback function transforms the element and the resulting
value is included in the new array.
Advanced Array Methods(.find(callback))

 // Finding the first fruit starting with 'B':


 let firstBFruit = fruits.find(fruit => fruit.startsWith('B'));
 console.log(firstBFruit); // Outputs: Banana
Advance Array Functions(.filter(callback))

 // Filtering fruits with length greater than 5:


 let longFruits = fruits.filter(fruit => fruit.length > 5);
 console.log(longFruits);// Outputs: [] (empty array, as no fruit is
longer than 5 characters)
Advance Array Functions(.map(callback))

// Creating a new array with fruits in uppercase:


let upperCaseFruits = fruits.map(fruit => fruit.toUpperCase());
console.log(upperCaseFruits); // Outputs: ["APPLE", "ORANGE",
"BANANA"]
Introduction to Strings
STRINGS IN TYPESCRIPT – POWERFUL TEXT
MANIPULATION
What are Strings?

 Sequence of characters: Strings represent a series of characters,


including letters, numbers, symbols, and whitespace.
 Immutable in TypeScript: Once created, a string's content cannot be
directly changed (modified).
Declaring Strings with Type Annotations

// String with explicit type annotation:


let message: string = "Hello, world!";

// Implicit string declaration:


let greeting = "Welcome";
Strings Concatenations (Using + Operator)

// Combining strings using the + operator:


let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName); // Outputs: John Doe
Strings Concatenations (Using Literals)

// Template literals allow for embedded expressions:


let age = 30;
let greeting = `Hello, my name is ${firstName} and I'm $
{age} years old.`;
console.log(greeting); // Outputs: Hello, my name is
John and I'm 30 years old.
Strings Methods

 length property: Returns the number of characters in the string.


 .toUpperCase() method: Converts the string to uppercase.
 .toLowerCase() method: Converts the string to lowercase.
 .trim() method: Removes leading and trailing whitespace characters.
 .startsWith(prefix) method: Checks if the string starts with a specific prefix.
 .endsWith(suffix) method: Checks if the string ends with a specific suffix.
 .indexOf(substring) method: Returns the index of the first occurrence of a substring
within the string, or -1 if not found.
 .lastIndexOf(substring) method: Returns the index of the last occurrence of a substring
within the string, or -1 if not found.
 .slice(start, end) method: Extracts a section of the string based on start and end indexes.
GitHub Repositories to Follow

 Panaverse TypeScript Repository by Sir Zia Khan


 https://github.com/panaverse/learn-typescript.git
 Learn GIT by Zeeshan Hanif
 https://www.youtube.com/watch?v=MiXAma2db8Y&list=PLKueo-cldy_HjRnPUL4G3pWHS7FREAizF&index=2
 TypeScripts Assignments
 https://github.com/panaverse/learn-typescript/tree/master/NODE_PROJECTS
 https://github.com/panaverse/learn-typescript/blob/master/NODE_PROJECTS/getting-started-exercises.md
 Class Repository
 https://github.com/fkhan79/giaic_fmk/

You might also like