Skip to content

Commit 5f54bcf

Browse files
committed
created function for stretch assignment
1 parent c247e8c commit 5f54bcf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lesson_06/benjaminscott/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Stretch Assignment
2+
3+
```
4+
function isValidAlphaAbbreviation(word: string, abbr: string): boolean {
5+
var letterToNum = { "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 };
6+
let wordIndex = 0;
7+
let abbrIndex = 0;
8+
9+
while (abbrIndex < abbr.length && wordIndex < word.length) {
10+
if (abbr[abbrIndex] === word[wordIndex]) {
11+
wordIndex++;
12+
13+
} else {
14+
const currentAbbrChar = abbr[abbrIndex];
15+
16+
if (currentAbbrChar < 'a' || currentAbbrChar > 'z') {
17+
return false;
18+
}
19+
20+
const targetNumber = letterToNum[currentAbbrChar];
21+
22+
wordIndex += targetNumber;
23+
}
24+
25+
abbrIndex++;
26+
}
27+
28+
if (abbrIndex === abbr.length && wordIndex === word.length) {
29+
return true;
30+
}
31+
return false;
32+
}
33+
```

0 commit comments

Comments
 (0)