Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
da6aa39
Tests passed to calculate word score
nkiruka Nov 15, 2017
0f297b8
Test passed to accept upper or lower case letters
nkiruka Nov 15, 2017
a98bb0e
Added logic for scoring 7 letter word
nkiruka Nov 15, 2017
ffe3b9d
Passed test scoring 7 letter word
nkiruka Nov 15, 2017
4a33fe3
Added logic to check if letter is null
nkiruka Nov 15, 2017
26cee6e
Added logic to select word with fewer tiles if there is tie
nkiruka Nov 15, 2017
c860ee0
Passing test for returning first word of a tie with same letter count
nkiruka Nov 15, 2017
82e9dd4
Passing tests for highestscore function'
nkiruka Nov 15, 2017
b7b16cd
Added logic to throw exception if word is greater than 7 letters
nkiruka Nov 15, 2017
8fad408
Add logic to throw exception if an empty string is passed
nkiruka Nov 15, 2017
024f84d
Used regex to validate string
nkiruka Nov 15, 2017
12ae92d
Add constructor to Player class
nkiruka Nov 16, 2017
ee265ae
Passing test for Player function
nkiruka Nov 16, 2017
6f1de1e
Add error proof when name is undefined
nkiruka Nov 16, 2017
7365134
Added plays property
nkiruka Nov 16, 2017
78ac533
Added plays arra
nkiruka Nov 16, 2017
23e9b99
Error handling for invalid word play
nkiruka Nov 16, 2017
9002acb
Added totalScore function and test passed
nkiruka Nov 16, 2017
3bd8d71
Passing tests for totalScore function returning the correct scores
nkiruka Nov 16, 2017
fd87719
Moved functionality for checking if player won from totalScore functi…
nkiruka Nov 16, 2017
db29d0e
All tests for hasWon function passing
nkiruka Nov 16, 2017
d04984b
Tests pass for checking if player has won
nkiruka Nov 16, 2017
450bac2
Added highestScoringWord function
nkiruka Nov 16, 2017
36b9b05
All tests passing for highestScoringWord function
nkiruka Nov 16, 2017
3982f51
Added logic for highest word score
nkiruka Nov 16, 2017
f522981
All tests for highest word score passing
nkiruka Nov 16, 2017
86c10bd
Woo hoo fixed the scoring bug, every test passes
nkiruka Nov 16, 2017
06cb109
Formatting code
nkiruka Nov 16, 2017
3f9f85f
Fixed indentation & semi-colons
nkiruka Nov 16, 2017
370753e
Format code
nkiruka Nov 16, 2017
51f48c8
Added stub for arrow function
nkiruka Nov 17, 2017
9be034d
Completed final revisions
nkiruka Nov 17, 2017
38e5935
revision complete
nkiruka Nov 17, 2017
6ffecb2
merge conflict
nkiruka Nov 17, 2017
bfa5f12
submit final changes
nkiruka Nov 17, 2017
9cab762
fixed indentation
nkiruka Nov 17, 2017
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.

156 changes: 150 additions & 6 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,158 @@
const tiles = {
'A': 1,
'E': 1,
'I': 1,
'O': 1,
'L': 1,
'N': 1,
'R': 1,
'U': 1,
'S': 1,
'T': 1,
'D': 2,
'G': 2,
'B': 3,
'C': 3,
'M': 3,
'P': 3,
'F': 4,
'H': 4,
'V': 4,
'W': 4,
'Y': 4,
'K': 5,
'J': 8,
'X': 8,
'Q': 10,
'Z': 10
};

const Scrabble = {
score: function(word) {
// TODO: implement score
}
let regex = /^[a-zA-Z]+$/;
let total = 0;
let playedWord = word.toUpperCase();

// TODO: add the highestScoreFrom method
if (playedWord.length > 7) {
throw new UserException(`Illegal play, ${playedWord.length} exceeds allowed tile count`);
}

if (playedWord === '') {
throw new UserException(`Illegal play ${playedWord} contains bad characters`);
}

if (!playedWord.match(regex) ) {
throw new UserException('Illegal play, sorry only words allowed');
}

word.toUpperCase().split('').forEach(function(letter) {
total = tiles[letter] + total;
});

if (word.length == 7 && total > 0) {
total += 50;
}
return total;
},

highestScoreFrom: function(arrayOfWords) {
let max = 0;
let highestScoreWord = '';

if (arrayOfWords.length === 0) {
throw new Error('No words to score');
} else if (arrayOfWords.length === 1){
return arrayOfWords[0];
}

arrayOfWords.forEach(function(word) {
let wordScore = Scrabble.score(word)
let wordLength = word.length

if ( wordScore > max ) {
max = wordScore;
highestScoreWord = word;
} else if ( wordScore === max ) {
if ( wordLength === 7 ) {
// console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word);
return highestScoreWord = word;
} else if (highestScoreWord.length === 7 ) {
// console.log('work ' + highestScoreWord);
return highestScoreWord;
} else if (wordLength === highestScoreWord.length ) {
// console.log('work ' + highestScoreWord);
return highestScoreWord;
} else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) {
return highestScoreWord = word;
}
}
});
return highestScoreWord;
}
}; // end of Scrabble function

};

Scrabble.Player = class {
// TODO: implement the Player class
};
constructor(name) {
if (name === undefined) {
throw new Error('No argument passed');
}
this.name = name; // return player's name
this.plays = []; // store words played
}

play(word) {
let regex = /^[a-zA-Z]+$/;

if (this.hasWon()) {
return false;
}

if ((word === undefined) || (!word.match(regex))) {
throw new Error('Invalid word');
}

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

// totalScore() {
// let total = 0;
// this.plays.forEach(function(playedWord) {
// total += Scrabble.score(playedWord)
// });
// return total;
// }

// arrow function
totalScore() {
return this.plays.reduce((total, playedWord) => { return total + Scrabble.score(playedWord) }, 0);
}

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

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

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

// Optional Enhancements
// Scrabble.TileBag = class {
//
// }
module.exports = Scrabble;

//tests
// let name = 'Bob';
// let player = new Scrabble.Player(name);
// console.log(player.name)
3 changes: 2 additions & 1 deletion spec/scrabble_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('highestScoreFrom', function() {
expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig');
});


it ('if tied, prefer a word with 7 letters', function() {
const loser = 'zzzzzz';
const winner = 'iiiiddd';
Expand Down Expand Up @@ -147,7 +148,7 @@ describe('Player', function() {
expect(player.plays.length).toBe(1);
});
});

//
describe ('totalScore', function() {
it ('Is zero if the player has not played anything', function() {
let player = new Scrabble.Player('test player');
Expand Down