Share
Explore

# JavaScript Lab NotebookJavaScript Lab Notebook

References:
Learning outcomes:
These exercises provide a comprehensive understanding of arrays, object-oriented programming, and the use of functions in TypeScript with Node.js.

### Lab Exercise 1: Basic Array Functions

Objective: Understand basic array functions like push, pop, shift, and unshift.
typescriptCopy code
let fruits: string[] = [];
fruits.push("apple");
fruits.push("banana");
fruits.unshift("grape");
console.log(fruits); // Expected: ['grape', 'apple', 'banana']

### Lab Exercise 2: Searching in an Array

Objective: Use the indexOf and includes functions to search in an array.
typescriptCopy code
let numbers: number[] = [10, 20, 30, 40, 50];
console.log(numbers.indexOf(30)); // Expected: 2
console.log(numbers.includes(25)); // Expected: false

### Lab Exercise 3: Sorting an Array

Objective: Sort an array using the sort function.
typescriptCopy code
let names: string[] = ["Zoe", "Anna", "Charlie"];
names.sort();
console.log(names); // Expected: ['Anna', 'Charlie', 'Zoe']

### Lab Exercise 4: Joining Arrays

Objective: Use the join function to convert an array to a string.
typescriptCopy code
let colors: string[] = ["red", "green", "blue"];
console.log(colors.join(", ")); // Expected: 'red, green, blue'

### Lab Exercise 5: Creating a Class

Objective: Create a simple class and instantiate an object.
typescriptCopy code
class Car {
constructor(public brand: string, public model: string) {}
}

let myCar = new Car("Toyota", "Camry");
console.log(myCar.brand); // Expected: 'Toyota'

### Lab Exercise 6: Class Inheritance

Objective: Understand inheritance by extending a class.
typescriptCopy code
class Vehicle {
constructor(public type: string) {}
}

class Bike extends Vehicle {
constructor(public brand: string) {
super("Bike");
}
}

let myBike = new Bike("Yamaha");
console.log(myBike.type); // Expected: 'Bike'

### Lab Exercise 7: Polymorphism

Objective: Use polymorphism to override methods.
typescriptCopy code
class Animal {
speak(): string {
return "Some sound";
}
}

class Dog extends Animal {
speak(): string {
return "Woof!";
}
}

let dog = new Dog();
console.log(dog.speak()); // Expected: 'Woof!'

### Lab Exercise 8: Storing Objects in an Array

Objective: Create multiple objects and store them in an array.
typescriptCopy code
class Student {
constructor(public name: string, public age: number) {}
}

let students: Student[] = [
new Student("Alice", 20),
new Student("Bob", 22)
];
console.log(students[0].name); // Expected: 'Alice'

### Lab Exercise 9: Iterating Over Objects in an Array

Objective: Use the forEach function to iterate over objects in an array.
typescriptCopy code
students.forEach(student => {
console.log(`\${student.name} is \${student.age} years old.`);
});

### Lab Exercise 10: Using Map with Objects

Objective: Convert an array of objects to another array using the map function.
typescriptCopy code
let ages: number[] = students.map(student => student.age);
console.log(ages); // Expected: [20, 22]

## Lab Exercises: Series 2:

Apply exceptions, error handling, Iterators and Generators.
4.1 Incorporate exception handling with try and catch
4.2 Work with the Error object and throw errors
4.3 Understand try-catch-finally block of statements
4.4 Use the iteration protocol and generators

### Lab Exercise 1: Basic Exception Handling

Objective: Understand how to use try and catch to handle exceptions.
typescriptCopy code
try {
let result = 10 / 0; // This won't actually throw an error, but for demonstration purposes.
} catch (error) {
console.error("An error occurred:", error.message);
}

### Lab Exercise 2: Throwing Errors

Objective: Learn how to throw custom errors using the Error object.
typescriptCopy code
function divide(a: number, b: number): number {
if (b === 0) {
throw new Error("Division by zero is not allowed.");
}
return a / b;
}

try {
let result = divide(10, 0);
} catch (error) {
console.error(error.message); // Expected: 'Division by zero is not allowed.'
}

### Lab Exercise 3: Using Finally Block

Objective: Understand the finally block and its use cases.
typescriptCopy code
try {
let result = 10 / 5;
console.log(result);
} catch (error) {
console.error("An error occurred:", error.message);
} finally {
console.log("This will always run, regardless of an error.");
}

### Lab Exercise 4: Iteration Protocol

Objective: Use the iteration protocol to create a custom iterator.
typescriptCopy code
class Range {
constructor(public start: number, public end: number) {}

[Symbol.iterator]() {
let current = this.start;
let end = this.end;

return {
next() {
if (current <= end) {
return { value: current++, done: false };
} else {
return { done: true };
}
}
};
}
}

let range = new Range(1, 5);
for (let num of range) {
console.log(num); // Expected: 1, 2, 3, 4, 5
}

### Lab Exercise 5: Basic Generator Function

Objective: Understand how to create and use a generator function.
typescriptCopy code
function* numberGenerator() {
yield 1;
yield 2;
yield 3;
}

for (let num of numberGenerator()) {
console.log(num); // Expected: 1, 2, 3
}

### Lab Exercise 6: Generator with Parameters

Objective: Use parameters with generator functions.
typescriptCopy code
function* rangeGenerator(start: number, end: number) {
while (start <= end) {
yield start++;
}
}

for (let num of rangeGenerator(1, 3)) {
console.log(num); // Expected: 1, 2, 3
}

### Lab Exercise 7: Error Handling with Generators

Objective: Handle errors within generator functions.
typescriptCopy code
function* errorGenerator() {
try {
yield "Start";
throw new Error("Error in generator");
yield "End"; // This won't be executed
} catch (error) {
yield error.message;
}
}

for (let value of errorGenerator()) {
console.log(value); // Expected: 'Start', 'Error in generator'
}

### Lab Exercise 8: Using Return with Generators

Objective: Understand the use of the return statement in generators.
typescriptCopy code
function* returnGenerator() {
yield "Start";
return "Returned Value";
yield "End"; // This won't be executed
}

for (let value of returnGenerator()) {
console.log(value); // Expected: 'Start'
}

### Lab Exercise 9: Combining Generators

Objective: Combine multiple generators using the yield* expression.
typescriptCopy code
function* generatorA() {
yield 1;
yield 2;
}

function* generatorB() {
yield* generatorA();
yield 3;
}

for (let value of generatorB()) {
console.log(value); // Expected: 1, 2, 3
}

### Lab Exercise 10: Async Generators

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP