-
Couldn't load subscription status.
- Fork 169
Snow Leopards | Anika SW #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
971b6b7
aa495da
6931af8
0cf8645
d2d175a
4ff71e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,232 @@ | ||
| const TOTAL_TILES = 98 | ||
| const STARTING_NUMBER = 1 | ||
| const MAX_DRAW = 10 | ||
|
|
||
| const WEIGHTED_LETTERS = { | ||
| 1: 'A', | ||
| 2: 'A', | ||
| 3: 'A', | ||
| 4: 'A', | ||
| 5: 'A', | ||
| 6: 'A', | ||
| 7: 'A', | ||
| 8: 'A', | ||
| 9: 'A', | ||
| 10: 'B', | ||
| 11: 'B', | ||
| 12: 'C', | ||
| 13: 'C', | ||
| 14: 'D', | ||
| 15: 'D', | ||
| 16: 'D', | ||
| 17: 'D', | ||
| 18: 'E', | ||
| 19: 'E', | ||
| 20: 'E', | ||
| 21: 'E', | ||
| 22: 'E', | ||
| 23: 'E', | ||
| 24: 'E', | ||
| 25: 'E', | ||
| 26: 'E', | ||
| 27: 'E', | ||
| 28: 'E', | ||
| 29: 'E', | ||
| 30: 'F', | ||
| 31: 'F', | ||
| 32: 'G', | ||
| 33: 'G', | ||
| 34: 'G', | ||
| 35: 'H', | ||
| 36: 'H', | ||
| 37: 'I', | ||
| 38: 'I', | ||
| 39: 'I', | ||
| 40: 'I', | ||
| 41: 'I', | ||
| 42: 'I', | ||
| 43: 'I', | ||
| 44: 'I', | ||
| 45: 'I', | ||
| 46: 'J', | ||
| 47: 'K', | ||
| 48: 'L', | ||
| 49: 'L', | ||
| 50: 'L', | ||
| 51: 'L', | ||
| 52: 'M', | ||
| 53: 'M', | ||
| 54: 'N', | ||
| 55: 'N', | ||
| 56: 'N', | ||
| 57: 'N', | ||
| 58: 'N', | ||
| 59: 'N', | ||
| 60: 'O', | ||
| 61: 'O', | ||
| 62: 'O', | ||
| 63: 'O', | ||
| 64: 'O', | ||
| 65: 'O', | ||
| 66: 'O', | ||
| 67: 'O', | ||
| 68: 'P', | ||
| 69: 'P', | ||
| 70: 'Q', | ||
| 71: 'R', | ||
| 72: 'R', | ||
| 73: 'R', | ||
| 74: 'R', | ||
| 75: 'R', | ||
| 76: 'R', | ||
| 77: 'S', | ||
| 78: 'S', | ||
| 79: 'S', | ||
| 80: 'S', | ||
| 81: 'T', | ||
| 82: 'T', | ||
| 83: 'T', | ||
| 84: 'T', | ||
| 85: 'T', | ||
| 86: 'T', | ||
| 87: 'U', | ||
| 88: 'U', | ||
| 89: 'U', | ||
| 90: 'U', | ||
| 91: 'V', | ||
| 92: 'V', | ||
| 93: 'W', | ||
| 94: 'W', | ||
| 95: 'X', | ||
| 96: 'Y', | ||
| 97: 'Y', | ||
| 98: 'Z' | ||
| }; | ||
|
|
||
| const LETTER_VALUES = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job using const. We can use const for mutable objects and still add/remove from the object or array, we just can't reassign what LETTER_VALUES references |
||
| '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 | ||
| } | ||
|
|
||
| // Helper functions // | ||
|
|
||
| const pickRandomNumber = () => Math.floor(Math.random() * TOTAL_TILES) + STARTING_NUMBER; | ||
|
|
||
| const calculateScore = (word) => { | ||
| let score = 0; | ||
| for (const letter of word) { | ||
| score = score + LETTER_VALUES[letter]; | ||
| } | ||
| if (word.length > 6) { | ||
| score = score + 8 | ||
| } | ||
| return score; | ||
| } | ||
|
|
||
|
|
||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| const keysArray = []; | ||
| const lettersInHand = []; | ||
|
|
||
| for (let i = 0; keysArray.length < MAX_DRAW; i++) { | ||
| const key = pickRandomNumber(); | ||
| if (!keysArray.includes(key)) { | ||
| keysArray.push(key); | ||
| } | ||
| } | ||
|
|
||
| for (const key of keysArray) { | ||
| let tileLetter = WEIGHTED_LETTERS[key]; | ||
| lettersInHand.push(tileLetter); | ||
| } | ||
|
|
||
|
Comment on lines
+156
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could these two for-loops be combined into one loop? How would your logic need to change so you could get a random key and push the associated letter into |
||
| return lettersInHand; | ||
| }; | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| const lettersInHandObject = {}; | ||
|
|
||
| for (const letter of lettersInHand) { | ||
| if (letter in lettersInHandObject) { | ||
| lettersInHandObject[letter] += 1; | ||
| } else { | ||
| lettersInHandObject[letter] = 1; | ||
| } | ||
| } | ||
|
|
||
| for (const letter of input) { | ||
| if (letter in lettersInHandObject) { | ||
| lettersInHandObject[letter] -= 1; | ||
| if (lettersInHandObject[letter] < 0) { | ||
| return false; | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
|
Comment on lines
+189
to
+191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What scenario is this else block supposed to handle? Do we need it? |
||
| } | ||
| return true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another approach for checking if the input uses available letters is to loop through input with a for loop and check that the letter at each index is included in for (let i = 0; i < input.length; i++) {
let letter = input[i];
if (lettersInHand.includes(letter)) {
let letterIndex = lettersInHand.indexOf(letter);
lettersInHand.splice(letterIndex, 1);
} else {
return false;
}
}
return true;
}; |
||
| }; | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| const wordUpper = word.toUpperCase(); | ||
| const score = calculateScore(wordUpper); | ||
|
|
||
| return score; | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| const winningWord = { | ||
| word: '', | ||
| score: 0 | ||
| } | ||
|
|
||
| for (const word of words) { | ||
| const wordScore = calculateScore(word); | ||
| if (wordScore > winningWord.score) { | ||
| winningWord.word = word; | ||
| winningWord.score = wordScore; | ||
| } else if (wordScore === winningWord.score) { | ||
| if (word.length === 10 || winningWord.word.length === 10) { | ||
| if (winningWord.word.length === 10) { | ||
| continue; | ||
| } else { | ||
| winningWord.word = word; | ||
| winningWord.score = wordScore; | ||
| } | ||
| } else { | ||
| if (word.length < winningWord.word.length) { | ||
| winningWord.word = word; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return winningWord; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two notes here:
A way to keep your projects organized is to put large constant variables like this in a separate file and then import the data from that file into the places it needs to be used. Here's an example of how to do it.
The other thing is, what if you had a dictionary where the key was a letter and the value was the frequency of a letter {A: 9, B: 2, C: 2}. How would you iterate over this object to create a list of 98 letters to represent the frequency distribution?