Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lesson_06/expression/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your assigned version here
HW_VERSION=D
23 changes: 22 additions & 1 deletion lesson_06/expression/src/expression_calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@ export class ExpressionCalculator {
/** Returns a calculation involving a, b, c, d, and e */
calculate(a: number, b: number, c: number, d: number, e: number): number {
// Implement your code here to return the correct value.
return 0;
// Function to implement: a * b / MAtjh.pow(c + d, e)

const answer = this.divide(
this.multiply(a, b),
this.pow(this.add(c, d), e),
);
return answer;
}

add(a: number, b: number): number {
return a + b;
}

multiply(a: number, b: number): number {
return a * b;
}

divide(a: number, b: number): number {
if (b === 0) {
throw new Error("Division by zero is not allowed.");
}
return a / b;
}

pow(base: number, exponent: number): number {
Expand Down
75 changes: 75 additions & 0 deletions lesson_06/tyranricejr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# isValidAlphaAbbreviation Functionality in TypeScript

This markdown file documents the implementation of a function that validates whether an abbreviation is a correct ***alpha abbreviation*** of a given word. It includes code snippets, test cases to verify correctness, and an explanation of the logic used. The document also highlights key considerations when working with TypeScript vs. plain JavaScript.

## isValidAlphaAbbreviation.ts

### Params:
- word: string → the original word to validate against.
- abbr: string → the abbreviation sequence to check.

### Return:
- boolean → true if the abbreviation correctly represents the word, false otherwise.

```typescript
function isValidAlphaAbbreviation(word: string, abbr: string): boolean {
if(word == "" || abbr == "") return false;
if(word.length < abbr.length) return false;

// Letters mapped to numeric skip values (a=1, ..., z=26)
const AlphaToNumberValues = {
"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8, "i": 9,
"j": 10, "k": 11, "l": 12, "m": 13, "n": 14, "o": 15, "p": 16, "q": 17,
"r": 18, "s": 19, "t": 20, "u": 21, "v": 22, "w": 23, "x": 24,
"y": 25, "z": 26
};

let wordIndex = 0;
let isAbbreviation = true;
for (let abbrIndex = 0; abbrIndex < abbr.length; abbrIndex++) {
// Even indices: literal character match
if(abbrIndex % 2 == 0) {
if(abbr[abbrIndex] != word[wordIndex]) {
isAbbreviation = false;
break;
}
else {
wordIndex++;
}
}
// Odd indices: skip value
else if(abbrIndex % 2 == 1) {
wordIndex += AlphaToNumberValues[abbr[abbrIndex]];
}
}

return isAbbreviation && wordIndex === word.length;
}
```

### Example Test Cases:
```typescript
console.log("\n--- Test Case 1 ---")
console.log("isValidAlphaAbbreviation(word: internationalization, abbr: imzdn) Should Return True")
console.log("Result: ", isValidAlphaAbbreviation("internationalization", "imzdn"))

console.log("\n--- Test Case 2 ---")
console.log("isValidAlphaAbbreviation(word: internationalization, abbr: imz) Should Return False")
console.log("Result: ", isValidAlphaAbbreviation("internationalization", "imz"))

console.log("\n--- Test Case 3 ---")
console.log("isValidAlphaAbbreviation(word: substitution, abbr: sjn) Should Return True")
console.log("Result: ", isValidAlphaAbbreviation("substitution", "sjn"))

console.log("\n--- Test Case 4 ---")
console.log("isValidAlphaAbbreviation(word: substitution, abbr: san) Should Return False")
console.log("Result: ", isValidAlphaAbbreviation("substitution", "san"))

console.log("\n--- Test Case 5 ---")
console.log("isValidAlphaAbbreviation(word: test, abbr: ab) Should Return False")
console.log("Result: ", isValidAlphaAbbreviation("test", "ab"))

console.log("\n--- Test Case 6 ---")
console.log("isValidAlphaAbbreviation(word: test, abbr: tbt) Should Return True")
console.log("Result: ", isValidAlphaAbbreviation("test", "tbt"))
```