Skip to content

Instantly share code, notes, and snippets.

@caike
Created October 25, 2010 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/645011 to your computer and use it in GitHub Desktop.
Save caike/645011 to your computer and use it in GitHub Desktop.
prs.js
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;
};
};
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