From cfc90686ed90929ba25b88d14e69b84dc58c949f Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Thu, 29 Feb 2024 10:27:53 +0100 Subject: [PATCH 1/3] pass core and extension --- pseudocode.md | 27 +++++- src/scrabble.js | 216 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 237 insertions(+), 6 deletions(-) diff --git a/pseudocode.md b/pseudocode.md index e279f59..9b339bf 100644 --- a/pseudocode.md +++ b/pseudocode.md @@ -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. + +... ``` diff --git a/src/scrabble.js b/src/scrabble.js index 0d18249..c818397 100644 --- a/src/scrabble.js +++ b/src/scrabble.js @@ -1,5 +1,217 @@ -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 +function scrabble(word) { + let total = 0 + const doubleOrTripleWord = doubleAndTripleCheck(word) + const validWordCheck = checkWord(word) + + if (validWordCheck === false) { + total = 0 + return total + } + + if ( + doubleOrTripleWord.doubleWord === true || + doubleOrTripleWord.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 +function checkWord(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) { + validWordCheck = false + } + + return validWordCheck +} + +// Check if word contains any brackets and calculate the correspoding score if they do +function detectBracket(word) { + let total = 0 + word = word.toUpperCase() + + if (word === '' || word === null) { + total = 0 + } else if (word.includes('{') === true && word.includes('}') === true) { + const re = /[^{]+(?=\})/g + const found = word.match(re) + word = found.toString() + for (const letter of found) { + total += values[letter] * 2 + } + } else if (word.includes('[') === true && word.includes(']') === true) { + const re = /(?<=\[).+?(?=\])/g + const found = word.match(re) + word = found.toString() + for (const letter of word) { + total += values[letter] * 3 + } + } else { + total = 0 + } + + return total +} + +// If the word contains any brackets, split the word and take out the brackets +function splitWords(word) { + word = word.toUpperCase() + + if (word.includes('{') === true && word.includes('}') === true) { + const tempWord = word.split('{') + const tempWord2 = tempWord[1].split('}') + word = tempWord[0] + tempWord2[1] + } else if (word.includes('[') === true && word.includes(']') === true) { + const tempWord = word.split('[') + const tempWord2 = tempWord[1].split(']') + word = tempWord[0] + tempWord2[1] + } else { + return word + } + + return word +} + +// Check if the word contains double and triple brackets +function doubleAndTripleCheck(word) { + const doubleAndTripleWords = { doubleWord: false, tripleWord: false } + + if (word === '' || word === null) { + 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 +function caculateDoubleAndTripleWordScore(word) { + let doubleOrTripleWord = false + let total = 0 + doubleOrTripleWord = doubleAndTripleCheck(word) + word = word.toUpperCase() + + if (doubleOrTripleWord.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 + } + } 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 (doubleOrTripleWord.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 From f153f24da142988c639198e0b06954c341faa314 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Thu, 29 Feb 2024 11:24:34 +0100 Subject: [PATCH 2/3] cleaned up code a bit --- src/scrabble.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/scrabble.js b/src/scrabble.js index c818397..fd8e4c9 100644 --- a/src/scrabble.js +++ b/src/scrabble.js @@ -27,7 +27,7 @@ const values = { Z: 10 } -// Scrabble function +// Scrabble function to calculate word score function scrabble(word) { let total = 0 const doubleOrTripleWord = doubleAndTripleCheck(word) @@ -46,6 +46,7 @@ function scrabble(word) { } else { total = detectBracket(word) word = splitWords(word) + for (const letter of word) { total += values[letter] } @@ -80,24 +81,22 @@ function detectBracket(word) { let total = 0 word = word.toUpperCase() - if (word === '' || word === null) { - total = 0 - } else if (word.includes('{') === true && word.includes('}') === true) { + 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('[') === true && word.includes(']') === true) { + } 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 } - } else { - total = 0 } return total @@ -107,11 +106,11 @@ function detectBracket(word) { function splitWords(word) { word = word.toUpperCase() - if (word.includes('{') === true && word.includes('}') === true) { + if (word.includes('{') && word.includes('}')) { const tempWord = word.split('{') const tempWord2 = tempWord[1].split('}') word = tempWord[0] + tempWord2[1] - } else if (word.includes('[') === true && word.includes(']') === true) { + } else if (word.includes('[') && word.includes(']')) { const tempWord = word.split('[') const tempWord2 = tempWord[1].split(']') word = tempWord[0] + tempWord2[1] @@ -156,10 +155,12 @@ function caculateDoubleAndTripleWordScore(word) { 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 } @@ -171,13 +172,17 @@ function caculateDoubleAndTripleWordScore(word) { 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 (doubleOrTripleWord.tripleWord === true) { @@ -185,10 +190,12 @@ function caculateDoubleAndTripleWordScore(word) { 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 } @@ -200,13 +207,16 @@ function caculateDoubleAndTripleWordScore(word) { 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 } } From b6e5e06e01c1d5cd0eeacc68b198c1ac8c22c828 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Thu, 29 Feb 2024 16:41:55 +0100 Subject: [PATCH 3/3] add more explanations in the comments and change code readability --- src/scrabble.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/scrabble.js b/src/scrabble.js index fd8e4c9..251beba 100644 --- a/src/scrabble.js +++ b/src/scrabble.js @@ -28,10 +28,14 @@ const values = { } // 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 doubleOrTripleWord = doubleAndTripleCheck(word) - const validWordCheck = checkWord(word) + const doublePlusTripleWord = doubleAndTripleCheck(word) + const validWordCheck = checkIfWordIsWord(word) if (validWordCheck === false) { total = 0 @@ -39,8 +43,8 @@ function scrabble(word) { } if ( - doubleOrTripleWord.doubleWord === true || - doubleOrTripleWord.tripleWord === true + doublePlusTripleWord.doubleWord === true || + doublePlusTripleWord.tripleWord === true ) { total = caculateDoubleAndTripleWordScore(word) } else { @@ -56,7 +60,11 @@ function scrabble(word) { } // Check if word is an actual word, without special characters -function checkWord(word) { +// 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 = /[`!@#$%^&*()_+\-=;':"\\|,.<>/?~]/ @@ -77,6 +85,9 @@ function checkWord(word) { } // 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() @@ -103,6 +114,8 @@ function detectBracket(word) { } // 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() @@ -122,6 +135,9 @@ function splitWords(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 } @@ -145,13 +161,18 @@ function doubleAndTripleCheck(word) { } // 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 doubleOrTripleWord = false + let doublePlusTripleWord = false let total = 0 - doubleOrTripleWord = doubleAndTripleCheck(word) + doublePlusTripleWord = doubleAndTripleCheck(word) word = word.toUpperCase() - if (doubleOrTripleWord.doubleWord === true) { + if (doublePlusTripleWord.doubleWord === true) { const re = /[^{]+(?=\})/g const found = word.match(re) word = found.toString() @@ -185,7 +206,7 @@ function caculateDoubleAndTripleWordScore(word) { total *= 2 } - } else if (doubleOrTripleWord.tripleWord === true) { + } else if (doublePlusTripleWord.tripleWord === true) { const re = /(?<=\[).+?(?=\])/g const toUpperCase = word.toUpperCase() const found = toUpperCase.match(re)