Skip to content
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
27 changes: 23 additions & 4 deletions pseudocode.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
#### Add the steps you need to take to implement the requirements for Scrabble Challenge

```
1.
CORE
1. Assign each letter a value. Do this by creating an object with arrays inside.

2.
2. Loop through each letter in a word.

3.
3. Loop through each letter in the array inside the object

...etc
4. Check if the word[i] is equal to the object.array[j]

5. If they are equal: add the value of each letter to a total

6. Make it into a function and return the total

7. Account for empty strings or null

8. Account for lower case and upper case letters


EXTENDED
1. Detect if a letter is encased in {} or []

2. If they are encased, modify the score by doubling or tripling the letter score

3.

...
```
247 changes: 245 additions & 2 deletions src/scrabble.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,248 @@
function scrabble() {
// write code here
const values = {
A: 1,
B: 3,
C: 3,
D: 2,
E: 1,
F: 4,
G: 2,
H: 4,
I: 1,
J: 8,
K: 5,
L: 1,
M: 3,
N: 1,
O: 1,
P: 3,
Q: 10,
R: 1,
S: 1,
T: 1,
U: 1,
V: 4,
W: 4,
X: 8,
Y: 4,
Z: 10
}

// Scrabble function to calculate word score
// First check if the word is a valid word: if validWordCheck is false, return total is 0
// Then check if the word contains a double and triple word score. If it does, the total should be what is calculated in calculateDoubleAndTripleWordScore
// If none of this is the case, the word should be a normal word or a word with single brackets. In that case, the total should start with what is calculated in detectBracket and the word should be split. Where the double letter has been taken out. Then the normal word score can be calculated.
// In the end, the total should be returned
function scrabble(word) {
let total = 0
const doublePlusTripleWord = doubleAndTripleCheck(word)
const validWordCheck = checkIfWordIsWord(word)

if (validWordCheck === false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(!validWordCheck)

total = 0
return total
Comment on lines +41 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 0

}

if (
doublePlusTripleWord.doubleWord === true ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const isDoubleWord

doublePlusTripleWord.tripleWord === true
) {
total = caculateDoubleAndTripleWordScore(word)
} else {
total = detectBracket(word)
word = splitWords(word)

for (const letter of word) {
total += values[letter]
}
}

return total
}

// Check if word is an actual word, without special characters
// First check if the word does not cotain an empty string, null or whitespace
// Then check if the word contains a single bracket instead of two
// Lastly, check if the word contains any special characters
// If any of this is true, return validWordCheck is false, else return validWordCheck is true
function checkIfWordIsWord(word) {
let validWordCheck = true
const specialChars = /[`!@#$%^&*()_+\-=;':"\\|,.<>/?~]/

if (word === '' || word === null || word.includes(' ')) {
validWordCheck = false
} else if (
(word.includes('{') && !word.includes('}')) ||
(word.includes('}') && !word.includes('{')) ||
(word.includes('[') && !word.includes(']')) ||
(word.includes('[') && !word.includes(']'))
) {
validWordCheck = false
} else if (specialChars.test(word) === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(specialChars.test(word))

validWordCheck = false
}

return validWordCheck
}

// Check if word contains any brackets and calculate the correspoding score if they do
// If the word contains curly brackets, the double score for this letter should be calculated and added to the total
// If the word includes straight brackets, the triple score for this letter should be calculated and added to the total
// In the end, the total should be returned
function detectBracket(word) {
let total = 0
word = word.toUpperCase()

if (word.includes('{') && word.includes('}')) {
const re = /[^{]+(?=\})/g
const found = word.match(re)
word = found.toString()

for (const letter of found) {
total += values[letter] * 2
}
} else if (word.includes('[') && word.includes(']')) {
const re = /(?<=\[).+?(?=\])/g
const found = word.match(re)
word = found.toString()

for (const letter of word) {
total += values[letter] * 3
}
}

return total
}

// If the word contains any brackets, split the word and take out the brackets
// If the word contains any brackets, the new word, without the double or triple letter should be returned
// Else, the original word should be returned
function splitWords(word) {
word = word.toUpperCase()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save it to a new variable


if (word.includes('{') && word.includes('}')) {
const tempWord = word.split('{')
const tempWord2 = tempWord[1].split('}')
word = tempWord[0] + tempWord2[1]
} else if (word.includes('[') && word.includes(']')) {
const tempWord = word.split('[')
const tempWord2 = tempWord[1].split(']')
word = tempWord[0] + tempWord2[1]
} else {
return word
}
Comment on lines +130 to +132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded


return word
}

// Check if the word contains double and triple brackets
// If the word is an empty string or null, doubleWord and tripleWord should be false
// If the word is either a double word or triple word, then it should be true
// In the end, it should retrun doubleAndTripleWords
function doubleAndTripleCheck(word) {
const doubleAndTripleWords = { doubleWord: false, tripleWord: false }

if (word === '' || word === null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(!word)

return doubleAndTripleWords
} else if (
word[0] === '{' &&
word[word.length - 1] === '}' &&
word[2] !== '}'
) {
doubleAndTripleWords.doubleWord = true
} else if (
word[0] === '[' &&
word[word.length - 1] === ']' &&
word[2] !== ']'
) {
doubleAndTripleWords.tripleWord = true
}

return doubleAndTripleWords
}

// Calculate score for two word mulipliers
// If the word is a double score word it should be checked for having a triple word or letter score in the word as well. If it does, the corresponding score should be calculated
// If it does not have a triple word or letter score in there, the double word score should be calculated
// If the word is a triple score word it should be checked for having a double word or letter score in the word as well. If it does, the corresponding score should be calculated
// If it does not have a double word or letter score in there, the triple word score should be calculated
// It should return the calculated total
function caculateDoubleAndTripleWordScore(word) {
let doublePlusTripleWord = false
let total = 0
doublePlusTripleWord = doubleAndTripleCheck(word)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to first line let doublePlusTripleWord = doubleAndTripleCheck(word)

word = word.toUpperCase()

if (doublePlusTripleWord.doubleWord === true) {
const re = /[^{]+(?=\})/g
const found = word.match(re)
word = found.toString()

if (word[0] === '[' && word[word.length - 1] === ']') {
const re = /(?<=\[).+?(?=\])/g
const found = word.match(re)
word = found.toString()

for (const letter of word) {
total += values[letter] * 2 * 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Magic numbers", "Magic values"

}
} else if (word.indexOf('[') === -1 && word.indexOf(']') === -1) {
for (const letter of word) {
total += values[letter] * 2
}
} else {
const re = /(?<=\[).+?(?=\])/g
const found = word.match(re)
const newWord = found.toString()

for (const letter of newWord) {
total += values[letter] * 3
}

word = splitWords(word)

for (const letter of word) {
total += values[letter]
}

total *= 2
}
} else if (doublePlusTripleWord.tripleWord === true) {
const re = /(?<=\[).+?(?=\])/g
const toUpperCase = word.toUpperCase()
const found = toUpperCase.match(re)
word = found.toString()

if (word[0] === '{' && word[word.length - 1] === '}') {
const re = /[^{]+(?=\})/g
const found = word.match(re)
word = found.toString()

for (const letter of word) {
total += values[letter] * 2 * 3
}
} else if (word.indexOf('{') === -1 && word.indexOf('}') === -1) {
for (const letter of word) {
total += values[letter] * 3
}
} else {
const re = /[^{]+(?=\})/g
const found = word.match(re)
const newWord = found.toString()

for (const letter of newWord) {
total += values[letter] * 2
}
word = splitWords(word)

for (const letter of word) {
total += values[letter]
}

total *= 3
}
}

return total
}

module.exports = scrabble