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

Stef -- Carets #32

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.

131 changes: 127 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,137 @@
const LETTERVALUE = {
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
};


const Scrabble = {
score: function(word) {

Choose a reason for hiding this comment

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

You can also write this like:

score() {

// TODO: implement score
}

// TODO: add the highestScoreFrom method
let letters = word.toUpperCase().split('');
let wordScore = 0;

letters.forEach(function(letter) {
if (!(letter in LETTERVALUE)) {
throw 'Letters only!'
};
wordScore += LETTERVALUE[letter];
});

if (word.length > 7) {
throw 'Your word is too long, 7 letters or less please.';
} else if (word.length === 0) {
throw 'Your word is too short, did you even play any letters?';
} else if (word.length === 7) {
wordScore += 50;
};

return wordScore;
},

highestScoreFrom: function(arrayOfWords) {
let wordOne = arrayOfWords[0];
Copy link

@CheezItMan CheezItMan Nov 20, 2017

Choose a reason for hiding this comment

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

This only seems to work with an array of 2 words?
@SesameSeeds

let wordTwo = arrayOfWords[1];

if (arrayOfWords.length === 0 || !Array.isArray(arrayOfWords)) {
throw 'No words have been played. Absolutely none.';
} else if (arrayOfWords.length === 1) {
return wordOne;
};

let wordOneScore = this.score(wordOne);
let wordTwoScore = this.score(wordTwo);

if (wordOneScore > wordTwoScore) {
return wordOne;
} else if (wordOneScore < wordTwoScore) {
return wordTwo;
} else if (wordOneScore === wordTwoScore) {
if (wordOne.length === 7 ) {
return wordOne;
} else if (wordTwo.length === 7) {
return wordTwo;
} else if (wordOne.length < wordTwo.length) {
return wordOne;
} else if (wordTwo.length < wordOne.length) {
return wordTwo;
} else if (wordOne.length === wordTwo.length) {
return wordOne;
};
}
}
};

Scrabble.Player = class {
// TODO: implement the Player class
constructor(name) {
if (!name) {
throw 'A name please!'
};

this.name = name;
this.plays = [];
}

play(word) {
if (this.hasWon() === true) {
return false;
};

if (!word || typeof word !== 'string') {
throw 'That is not a valid word, please try again.'
};

this.plays.push(word);
return true;
}

totalScore() {
let total = 0;
for (let word of this.plays) {

Choose a reason for hiding this comment

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

This works well, using forEach is preferred however.

total += Scrabble.score(word);
};
return total;
}

hasWon() {
if(this.totalScore() >= 100) {
return true;
} else {
return false;
};
}

highestScoringWord() {
return Scrabble.highestScoreFrom(this.plays);
}

highestWordScore(){
return Scrabble.score(this.highestScoringWord());
}

};

module.exports = Scrabble;