fixed random variables

This commit is contained in:
Geir Okkenhaug Jerstad 2024-09-16 10:41:14 +02:00
parent af1354ff9c
commit 5e820e89a3
4 changed files with 74 additions and 6 deletions

View file

@ -0,0 +1,17 @@
function pickRandomAnswer(){
answerArray = [AffirmativeAnswers, nonCommittalAnswers, negativeAnswers];
randomAnswersArrary = answerArray[Math.floor(Math.random() * answerArray.length)];
randomAnswer = randomAnswersArrary[Math.floor(Math.random() * randomAnswersArrary.length)]
htmlAnswer = /*HTML*/ `
<div>Question: </div><br>
<div>Answer: ${randomAnswer}</div>
`;
clearScreen();
updateView();
}
function clearScreen(){
app.innerHTML = '';
}

View file

@ -1,2 +1,29 @@
const eightBall = [];
var app = document.getElementById('app')
const AffirmativeAnswers = [
'It is certain', 'It is decidedly so',
'Without a doubt', 'Yes definitely',
'You may rely on it', 'As I see it, yes',
'Most likely', 'Outlook good',
'Yes', 'Signs point to yes'
];
const nonCommittalAnswers = [
'Reply hazy, try again', 'Ask again later',
'Better not tell you now', 'Cannot predict now',
'Concentrate and ask again'
];
const negativeAnswers = [
'Don\t count on it', 'My reply is no',
'My sources say no', 'Outlook not so good',
'Very doubtful'
];
var html = /*HTML*/`
<div id="ball">8-ball</div>
Ask Your question: <input type="text" /><br>
<button onclick="pickRandomAnswer()">Submit</button>
`;
var htmlAnswer = '';
var answerArray = [];
var randomAnswersArrary = [];
var randomAnswer;

View file

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>QUnit</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.22.0.css">
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.22.0.js"></script>
<script src="controller.js"></script>
<script>
QUnit.test('Answer', function (assert) {
// arrange
let AffirmativeAnswers = ['hello'];
// act
let returnedText = pickRandomAnswer();
// assert
assert.equal(returnedText, 'hello');
});
</script>
</body>
</html>

View file

@ -1,7 +1,4 @@
updateView();
function updateView(){
let html = /*HTML*/ `
<div id="ball">8-ball</div>
`;
app.innerHTML = html;
app.innerHTML = html + htmlAnswer;
}