Skip to content
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
23 changes: 23 additions & 0 deletions lib/scrabble_score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ScrabbleScore
def self.scrabble_test(string)
if string == nil
0
else
string =string.downcase
letter_score={
/[aeioulnrst]/ => 1,
/[dg]/ => 2,
/[bcmp]/ => 3,
/[fhvmy]/ => 4,
/[k]/ => 5,
/[jx]/ => 8,
/[qz]/ => 10
}
word_score = 0
letter_score.each do |letter , value|
word_score += string.scan(letter).count * value
end
word_score
end
end
end
43 changes: 43 additions & 0 deletions spec/scrabble_score_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "scrabble_score"
describe ScrabbleScore do
describe ".scrabble_test"
context "given an empty string"
it "returns 0" do
expect(ScrabbleScore.scrabble_test("")).to eql(0)
end

context "given a space"
it "returns 0" do
expect(ScrabbleScore.scrabble_test(" ")).to eql(0)
end

context "given nil"
it "returns 0" do
expect(ScrabbleScore.scrabble_test(nil)).to eql(0)
end

context "given \"a\""
it "returns 1" do
expect(ScrabbleScore.scrabble_test("a")).to eql(1)
end

context "given \"f\""
it "returns 4" do
expect(ScrabbleScore.scrabble_test("f")).to eql(4)
end

context "given \"street\""
it "returns 6" do
expect(ScrabbleScore.scrabble_test("street")).to eql(6)
end

context "given \"OXYPHENBUTAZONE\""
it "returns 41" do
expect(ScrabbleScore.scrabble_test("OXYPHENBUTAZONE")).to eql(41)
end

context "given \"alarcity\""
it "returns 13" do
expect(ScrabbleScore.scrabble_test("alarcity")).to eql(13)
end
end