Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 143 additions & 2 deletions scrabble.js
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

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

let totalScore = 0;
let regex = /^[a-zA-Z]+$/;

word = word.toUpperCase();

if (regex.test(word) != true) {
throw 'Error';
}

Choose a reason for hiding this comment

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

Two comments here. First, instead of writing != true it would be more succinct to say if (!regex.test(word)).

Second, you should provide a more detailed description of what went wrong. Error by itself doesn't help much trying to debug.

// 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);
}

Choose a reason for hiding this comment

The 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:

  1. Calculate a score for every word
  2. Find those words that share the highest score
  3. Use apply tie-breaking logic to those words

A simpler algorithm is to iterate through the list of words, keeping track of the best word / score seen so far. For each word:

  • If the current word has a higher score than the best word, it's the new best word
  • If the best word has a higher score, keep it
  • If the two are tied, apply tie-breaking logic, favoring the existing best 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']);