Created
October 25, 2010 14:07
-
-
Save caike/645011 to your computer and use it in GitHub Desktop.
prs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function round(a, b) { | |
if (arguments.length != 2){ | |
return "fail"; | |
} | |
var matrix = { | |
paper:{winsAgainst:"rock"}, | |
rock:{winsAgainst:"scissors"}, | |
scissors:{winsAgainst:"paper"} | |
} | |
var tempA = a; | |
var tempB = b; | |
a = a.toLowerCase(); | |
b = b.toLowerCase(); | |
if (a == b) { | |
return 'tie'; | |
} | |
if (matrix[a].winsAgainst == b) { | |
return tempA; | |
} else { | |
return tempB; | |
}; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'A roshambo round' | |
it 'should take Paper and Rock and return Rock' | |
var winner = round('Paper', 'Rock'); | |
winner.should_be('Paper'); | |
end | |
it 'should take Rock and Paper and return Paper' | |
winner = round('Rock', 'Paper'); | |
winner.should_be('Paper'); | |
end | |
it 'should take Scissors and Paper and return Scissors' | |
winner = round('Scissors', 'Paper'); | |
winner.should_be('Scissors'); | |
end | |
it 'should take Paper and Scissors and return Scissors' | |
winner = round('Paper', 'Scissors'); | |
winner.should_be('Scissors'); | |
end | |
it 'should take Scissors and Rock and return Rock' | |
winner = round('Scissors', 'Rock'); | |
winner.should_be('Rock'); | |
end | |
it 'should take Rock and Scissors and return Rock' | |
winner = round('Rock', 'Scissors'); | |
winner.should_be('Rock'); | |
end | |
it 'should take rock and Scissors and return rock' | |
round('rock', 'Scissors').should_be('rock'); | |
end | |
it 'should take rock and rock and return tie' | |
round('rock', 'rock').should_be('tie'); | |
end | |
it 'should take no arguments and return fail' | |
-{round()}.should.throw_error InvalidArgument,'Invalid arguments'; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment