Skip to content

Instantly share code, notes, and snippets.

@natafaye
Last active February 9, 2022 02:51
Show Gist options
  • Save natafaye/d788fe53a24f61820d76050a3f345e6d to your computer and use it in GitHub Desktop.
Save natafaye/d788fe53a24f61820d76050a3f345e6d to your computer and use it in GitHub Desktop.
class AcceptedAnswerPrompt {
constructor(acceptedAnswers, ignoreCapitalization) {
this.acceptedAnswers = acceptedAnswers;
this.ignoreCapitalization = ignoreCapitalization;
}
checkIfAccepted(answer) {
// return true if it's in the list, false if it's not
for(const acceptedAnswer of this.acceptedAnswers) {
if(this.ignoreCapitalization) {
if( answer.toLowerCase() === acceptedAnswer.toLowerCase() ) {
return true;
}
}
else {
if( answer === acceptedAnswer ) {
return true;
}
}
// if (this.ignoreCapitalization && answer.toLowerCase() === acceptedAnswer.toLowerCase() || answer === acceptedAnswer) {
// return true;
// }
}
return false;
}
promptForAnswer(question) {
let answer = prompt(question);
while( !this.checkIfAccepted(answer) ) {
answer = prompt(question + " It needs to be in this list: " + this.acceptedAnswers.join(", ") );
}
return answer;
}
}
const expect = chai.expect;
describe("AcceptedAnswerPrompt", function() {
describe("#checkIfAccepted", function() {
it("should return false for answers that shouldn't be accepted", function() {
// Arrange
const yesNoPrompt = new AcceptedAnswerPrompt( ["yes", "no"], true);
// Act
const isAccepted = yesNoPrompt.checkIfAccepted("maybe");
// Assert
expect(isAccepted).to.equal(false);
})
it("should return true for answers that should be accepted", function() {
// Arrange
const yesNoPrompt = new AcceptedAnswerPrompt( ["yes", "no"], true);
// Act
const isAccepted = yesNoPrompt.checkIfAccepted("yes");
// Assert
expect(isAccepted).to.equal(true);
})
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"><p><a href=".">Index</a></p></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>
mocha.setup('bdd');
</script>
<script src="AcceptedAnswerPrompt.js"></script>
<script src="AcceptedAnswerPrompt.test.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment