-
Notifications
You must be signed in to change notification settings - Fork 41
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
pipes | tanisha | js-scrabble #33
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,155 @@ | ||
const Scrabble = { | ||
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, | ||
}, | ||
score: function(word) { | ||
// TODO: implement score | ||
// TODO: implement score //score(word): returns the total score value for the given word. The word is input as a string (case insensitive). The chart below shows the point value for a given letter | ||
|
||
let totalScore = 0; | ||
let regex = /^[a-zA-Z]+$/; | ||
|
||
word = word.toUpperCase(); | ||
|
||
if (regex.test(word) != true) { | ||
throw 'Error'; | ||
} | ||
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. Two comments here. First, instead of writing Second, you should provide a more detailed description of what went wrong. |
||
// score adds 50 points for a 7-letter word | ||
if (word.length == 7) { | ||
totalScore += 50; | ||
} else if (word.length > 7) { | ||
throw 'Error'; | ||
} | ||
// calculates score value for the given word | ||
for (let letter of word) { | ||
totalScore += this.values[letter]; | ||
} | ||
return totalScore; | ||
}, | ||
|
||
highestScoreFrom: function(arrayOfWords) { | ||
// returns the word in the array with the highest score | ||
let scores = []; | ||
// throw error if arrayOfWords is empty | ||
if (arrayOfWords.length == 0) { | ||
throw 'Error'; | ||
} | ||
// score each word and push onto scores array | ||
for (let word of arrayOfWords) { | ||
wordScore = this.score(word); | ||
scores.push(wordScore); | ||
} | ||
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. This algorithm ends up being a little over-complicated. By my reading, your steps are:
A simpler algorithm is to iterate through the list of words, keeping track of the best word / score seen so far. For each word:
This single-pass approach is more concise and (in my opinion) easier to understand. |
||
// find index of highest score | ||
highestScoreIndex = scores.indexOf(Math.max(...scores)); | ||
// find highest score | ||
highestScore = Math.max(...scores); | ||
|
||
let tieIndices = []; | ||
// loop through scores to find ties and capture indices of ties | ||
for (let i = 0; i < scores.length; i++) { | ||
if (scores[i] == highestScore) { | ||
tieIndices.push(i); | ||
} | ||
} | ||
|
||
|
||
// TODO: lines 58 - 87 do not work right now. I would to use a tieBreaker function to hold the tie rules and call it when i am scoring | ||
// // find shortest word | ||
// let shortestWord = function shortestWord(array) { | ||
// return array.reduce(function(shortestSoFar, current) { | ||
// if (current.length < shortestSoFar.length) { | ||
// return current; | ||
// } else { | ||
// return shortestSoFar; | ||
// } | ||
// }); | ||
// } | ||
// | ||
// let tieBreaker = function tieBreaker() { | ||
// // if tie word is seven, it wins | ||
// // if neither is seven, shortestWord wins | ||
// for (let i = 0; i < tieIndices.length; i++) { | ||
// if (arrayOfWords[tieIndices[i]].length == 7 ) { | ||
// return arrayOfWords[tieIndices[i]]; | ||
// } | ||
// } | ||
// return shortestWord; | ||
// } | ||
// | ||
// if (arrayOfWords.length < 2) { | ||
// return arrayOfWords[0]; | ||
// } else if (tieIndices >= 2) { | ||
// return tieBreaker(); | ||
// } else { | ||
// return arrayOfWords[highestScoreIndex]; | ||
// } | ||
// } | ||
for (let i = 0; i < tieIndices.length; i++) { | ||
if (arrayOfWords[tieIndices[i]].length == 7 ) { | ||
return arrayOfWords[tieIndices[i]]; | ||
} | ||
} | ||
|
||
if (arrayOfWords.length < 2) { | ||
return arrayOfWords[0]; | ||
} else { | ||
return arrayOfWords[highestScoreIndex]; | ||
} | ||
} | ||
|
||
// TODO: add the highestScoreFrom method | ||
|
||
}; | ||
|
||
Scrabble.Player = class { | ||
// TODO: implement the Player class | ||
constructor(name) { | ||
this.name = name; | ||
this.plays = []; | ||
if (this.name == null) { | ||
throw 'Error'; | ||
} | ||
} | ||
|
||
play(word) { | ||
if (this.hasWon() === true) { | ||
return false; | ||
} | ||
if (typeof word !== 'string') { | ||
throw 'Error'; | ||
} else { | ||
return this.plays.push(word); | ||
} | ||
} | ||
|
||
totalScore() { | ||
let total = 0; | ||
for (let word of this.plays) { | ||
total += Scrabble.score(word); | ||
} | ||
return total; | ||
} | ||
|
||
hasWon() { | ||
// returns `true` if the player has over 100 points, otherwise returns `false` | ||
if (this.totalScore() >= 100) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
highestScoringWord() { | ||
// method which returns the highest scoring word the user has played | ||
return Scrabble.highestScoreFrom(this.plays); | ||
} | ||
|
||
highestWordScore() { | ||
// returns the `highestScoringWord` score | ||
return Scrabble.score(Scrabble.highestScoreFrom(this.plays)); | ||
} | ||
}; | ||
|
||
module.exports = Scrabble; | ||
|
||
Scrabble.highestScoreFrom(['dog', 'pig','zzzzzz','iiiiddd']); |
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.
You should remove TODOs once they're done