From a72d62dd606d78652f61e92ddf6565925cd4db8f Mon Sep 17 00:00:00 2001 From: Adalbert Pungu <18pw419@esisalama.org> Date: Fri, 28 Jul 2023 09:30:46 +0200 Subject: [PATCH 1/3] Update Module 07 HTML file --- code/module-07/m07-end/module07.html | 2 +- code/module-07/m07-start/module07.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/code/module-07/m07-end/module07.html b/code/module-07/m07-end/module07.html index e2bfe13..b16f557 100644 --- a/code/module-07/m07-end/module07.html +++ b/code/module-07/m07-end/module07.html @@ -8,7 +8,7 @@

Test JavaScript

This page calls the script module08_main.js and is used for testing.

- + diff --git a/code/module-07/m07-start/module07.html b/code/module-07/m07-start/module07.html index e2bfe13..b16f557 100644 --- a/code/module-07/m07-start/module07.html +++ b/code/module-07/m07-start/module07.html @@ -8,7 +8,7 @@

Test JavaScript

This page calls the script module08_main.js and is used for testing.

- + From dd251cfc4cf127392a9bb6a73ffeae6e9ce7a592 Mon Sep 17 00:00:00 2001 From: Adalbert <59202588+AdalbertPungu@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:49:19 +0200 Subject: [PATCH 2/3] Update module05.ts calculation enhancement Improved calculation: the program would not compile if the table to be sorted or printed had more than 100 elements. I added a check in the constructor of the BuildArray class. By limiting to 100 to avoid performance or array size problems. --- code/module-05/m05-end/module05.ts | 33 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/code/module-05/m05-end/module05.ts b/code/module-05/m05-end/module05.ts index 22c1173..993b39a 100644 --- a/code/module-05/m05-end/module05.ts +++ b/code/module-05/m05-end/module05.ts @@ -10,7 +10,8 @@ class BuildArray { // TODO Define the constructor constructor (items: number, sortOrder: 'ascending' | 'descending') { - this._items = items; + // Limit the number of items to 100 if it exceeds 100 + this._items = Math.min(items, 100); this._sortOrder = sortOrder; } @@ -18,12 +19,16 @@ class BuildArray { get items() { return this._items; } + set items(items) { - this._items = items; + // Limit the number of items to 100 if it exceeds 100 + this._items = Math.min(items, 100); } + get sortOrder() { return this._sortOrder; } + set sortOrder(sortOrder) { this._sortOrder = sortOrder; } @@ -35,27 +40,33 @@ class BuildArray { } else if (b > a) { return 1; } else { - return 0;} - } + return 0; + } + } + private sortAscending = (a: number, b: number) => { if (a > b) { return 1; } else if (b > a) { return -1; } else { - return 0; } + return 0; + } } + buildArray(): number[] { let randomNumbers: number[] = []; let nextNumber: number; + for (let counter = 0; counter < this.items; counter++) { - nextNumber = Math.ceil(Math.random() * (100 - 1)); + nextNumber = Math.ceil(Math.random() * 100); // Removed (100 - 1) as it's redundant if (randomNumbers.indexOf(nextNumber) === -1) { randomNumbers.push(nextNumber); } else { counter--; } } + if (this._sortOrder === 'ascending') { return randomNumbers.sort(this.sortAscending); } else { @@ -66,8 +77,8 @@ class BuildArray { /* TODO: Instantiate the BuildArray objects. */ - let testArray1 = new BuildArray(12, 'ascending'); - let testArray2 = new BuildArray(8, 'descending'); - - console.log(testArray1.buildArray()); - console.log(testArray2.buildArray()); +let testArray1 = new BuildArray(12, 'ascending'); +let testArray2 = new BuildArray(8, 'descending'); + +console.log(testArray1.buildArray()); +console.log(testArray2.buildArray()); From 0a2bb6ad6ec5a722be11f4b2aa826b1808d44300 Mon Sep 17 00:00:00 2001 From: Adalbert <59202588+AdalbertPungu@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:54:02 +0200 Subject: [PATCH 3/3] Update module05.js during compilation Improved calculation: the program would not compile if the table to be sorted or printed had more than 100 elements. I added a check in the constructor of the BuildArray class. By limiting to 100 to avoid performance or array size problems. --- code/module-05/m05-end/build/module05.js | 103 ++++++++++++++--------- 1 file changed, 62 insertions(+), 41 deletions(-) diff --git a/code/module-05/m05-end/build/module05.js b/code/module-05/m05-end/build/module05.js index 54d4289..61da0e8 100644 --- a/code/module-05/m05-end/build/module05.js +++ b/code/module-05/m05-end/build/module05.js @@ -1,52 +1,73 @@ "use strict"; -// Comparison function that tells the sort method how to sort numbers in descending order -let sortDescending2 = (a, b) => { - if (a > b) { - return -1; - ; +/* Module 5: Declare and instantiate classes in TypeScript + Lab End */ +/* EXERCISE 1 */ +class BuildArray { + // TODO Define the constructor + constructor(items, sortOrder) { + // TODO Define the methods. + this.sortDescending = (a, b) => { + if (a > b) { + return -1; + } + else if (b > a) { + return 1; + } + else { + return 0; + } + }; + this.sortAscending = (a, b) => { + if (a > b) { + return 1; + } + else if (b > a) { + return -1; + } + else { + return 0; + } + }; + // Limit the number of items to 100 if it exceeds 100 + this._items = Math.min(items, 100); + this._sortOrder = sortOrder; } - else if (b > a) { - return 1; - ; + // TODO Define the accessors + get items() { + return this._items; } - else { - return 0; + set items(items) { + // Limit the number of items to 100 if it exceeds 100 + this._items = Math.min(items, 100); } -}; -// Comparison function that tells the sort method how to sort numbers in ascending order -let sortAscending2 = (a, b) => { - if (a > b) { - return 1; + get sortOrder() { + return this._sortOrder; } - else if (b > a) { - return -1; + set sortOrder(sortOrder) { + this._sortOrder = sortOrder; } - else { - return 0; - } -}; -// This function builds an array of unique random numbers containing the number of items based on the number passed to it. -// The sortOrder parameter determines whether to sort the array in ascending or decending order. -function buildArray2(items, sortOrder) { - let randomNumbers = []; - let nextNumber; - for (let i = 0; i < items; i++) { - nextNumber = Math.floor(Math.random() * (100 - 1)) + 1; - if (randomNumbers.indexOf(nextNumber) === -1) { - randomNumbers.push(nextNumber); + buildArray() { + let randomNumbers = []; + let nextNumber; + for (let counter = 0; counter < this.items; counter++) { + nextNumber = Math.ceil(Math.random() * 100); // Removed (100 - 1) as it's redundant + if (randomNumbers.indexOf(nextNumber) === -1) { + randomNumbers.push(nextNumber); + } + else { + counter--; + } + } + if (this._sortOrder === 'ascending') { + return randomNumbers.sort(this.sortAscending); } else { - i--; + return randomNumbers.sort(this.sortDescending); } } - if (sortOrder === 'ascending') { - return randomNumbers.sort(sortAscending2); - } - else { - return randomNumbers.sort(sortDescending2); - } } -let testArray1 = buildArray2(12, 'ascending'); -let testArray2 = buildArray2(8, 'descending'); -console.log(testArray1); -console.log(testArray2); +/* TODO: Instantiate the BuildArray objects. */ +let testArray1 = new BuildArray(12, 'ascending'); +let testArray2 = new BuildArray(8, 'descending'); +console.log(testArray1.buildArray()); +console.log(testArray2.buildArray());