-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca49843
commit 20726dc
Showing
4 changed files
with
121 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,64 @@ | ||
// Define the type for the object containing word and category | ||
export default class Word { | ||
category: string; | ||
text: string; | ||
id: number; | ||
category: string; | ||
text: string; | ||
id: number; | ||
|
||
static nextId: number = 0; | ||
static nextId: number = 0; | ||
|
||
constructor(category: string, text: string) { | ||
// Initialize Word with category and text | ||
this.category = category; | ||
this.text = text; | ||
this.id = Word.nextId++; | ||
} | ||
constructor(category: string, text: string) { | ||
// Initialize Word with category and text | ||
this.category = category; | ||
this.text = text; | ||
this.id = Word.nextId++; | ||
} | ||
|
||
// Override the toString method to return a unique value for each Word | ||
toString(): string { | ||
// Concatenate category and text to form a unique identifier | ||
return `${this.category}:${this.text}/${this.id}`; | ||
} | ||
// Override the toString method to return a unique value for each Word | ||
toString(): string { | ||
// Concatenate category and text to form a unique identifier | ||
return `${this.category}:${this.text}/${this.id}`; | ||
} | ||
|
||
// Compare objects based on their properties | ||
equivalent(other: Word | null): boolean { | ||
// Check if toString representations of both objects are equal | ||
return !!other && this.toString() === other.toString(); | ||
} | ||
// Compare objects based on their properties | ||
equivalent(other: Word | null): boolean { | ||
// Check if toString representations of both objects are equal | ||
return !!other && this.toString() === other.toString(); | ||
} | ||
|
||
// Compare objects based on their properties and id | ||
identical(other: Word | null): boolean { | ||
return !!other && this.equivalent(other) && this.id === other.id; | ||
} | ||
// Compare objects based on their properties and id | ||
identical(other: Word | null): boolean { | ||
return !!other && this.equivalent(other) && this.id === other.id; | ||
} | ||
|
||
// Check if the Word is in an array | ||
// Use this instead of arr.includes(this) | ||
isInArray(arr: Word[]): boolean { | ||
// Check if the Word exists in the array | ||
return this.indexInArray(arr) !== -1; | ||
} | ||
// Check if the Word is in an array | ||
// Use this instead of arr.includes(this) | ||
isInArray(arr: Word[]): boolean { | ||
// Check if the Word exists in the array | ||
return this.indexInArray(arr) !== -1; | ||
} | ||
|
||
// Get the index of the Word in an array or -1 if not found | ||
indexInArray(arr: Word[]): number { | ||
// Iterate through the array to find the index of the Word | ||
for (let i = 0; i < arr.length; i++) { | ||
if (this.equivalent(arr[i])) { | ||
// Return the index if Word is found | ||
return i; | ||
} | ||
} | ||
// Return -1 if Word is not found in the array | ||
return -1; | ||
// Get the index of the Word in an array or -1 if not found | ||
indexInArray(arr: Word[]): number { | ||
// Iterate through the array to find the index of the Word | ||
for (let i = 0; i < arr.length; i++) { | ||
if (this.equivalent(arr[i])) { | ||
// Return the index if Word is found | ||
return i; | ||
} | ||
} | ||
// Return -1 if Word is not found in the array | ||
return -1; | ||
} | ||
|
||
// Remove the Word from an array, if present | ||
removeFromArray(arr: Word[]): boolean { | ||
// Get the index of the Word in the array | ||
const index = this.indexInArray(arr); | ||
if (index !== -1) { | ||
// Remove the Word from the array if found | ||
arr.splice(index, 1); | ||
return true; // Return true indicating successful removal | ||
} | ||
return false; // Return false if Word is not found in the array | ||
// Remove the Word from an array, if present | ||
removeFromArray(arr: Word[]): boolean { | ||
// Get the index of the Word in the array | ||
const index = this.indexInArray(arr); | ||
if (index !== -1) { | ||
// Remove the Word from the array if found | ||
arr.splice(index, 1); | ||
return true; // Return true indicating successful removal | ||
} | ||
return false; // Return false if Word is not found in the array | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters