Skip to content

Update Module 07 HTML file and module05.ts calculation enhancement #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 62 additions & 41 deletions code/module-05/m05-end/build/module05.js
Original file line number Diff line number Diff line change
@@ -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());
33 changes: 22 additions & 11 deletions code/module-05/m05-end/module05.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ 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;
}

// TODO Define the accessors
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;
}
Expand All @@ -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 {
Expand All @@ -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());
2 changes: 1 addition & 1 deletion code/module-07/m07-end/module07.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1>Test JavaScript</h1>
<p id="date"></p>
<p>This page calls the script module08_main.js and is used for testing.</p>
<script src=".\build\module08_main.js"></script>
<script src=".\build\module07_main.js"></script>
<noscript>You need to enable JavaScript to view the full site.</noscript>
</body>
</html>
2 changes: 1 addition & 1 deletion code/module-07/m07-start/module07.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1>Test JavaScript</h1>
<p id="date"></p>
<p>This page calls the script module08_main.js and is used for testing.</p>
<script src=".\build\module08_main.js"></script>
<script src=".\build\module07_main.js"></script>
<noscript>You need to enable JavaScript to view the full site.</noscript>
</body>
</html>