0

I'm a Digital Arts student, I'm in my first year and recently we've been given the task to make a basic E-Learning website for maths. Basically you're asked what (for example) 7 times 7 is, you enter the answer and click a button, an alert window comes up. If you're correct it's supposed to say "correct" or "yay" or "well done" and if you're wrong it says "No" or "try again" or something else.

My problem is that it's supposed to show one random message in the window alert, not all of them at once or one after the other, just one randomly. I've looked but I wasn't able to find anything that helps.

Here's the part of the code I'm having trouble with:

<script type="text/javascript">
    var x, y;
    var answer;



    function aNumber() {

        return Math.floor(1 + (Math.random() * 12));

    }

    function genQuestion() {
        x = aNumber();
        y = aNumber();
        din = document.getElementById("inputVal");
        din.value = x + " times " + y;
        answer = x * y;
    }



    function ClickAnswer() {
        if (answer == document.getElementById("outputVal").value) {
            window.alert("Correct");
            window.alert("Yes!");
            window.alert("Well done");
           location.reload();

        } else {
            window.alert("No, try again.");
            window.alert("try again");
            window.alert("wrong");
        }

    }

    function displayArea() {
        din = document.getElementById("inputVal");
        dout = document.getElementById("outputVal");
        dout.value = circleArea(din.value);
    }

</script>
nefarious
  • 3
  • 6

4 Answers4

3

Put the messages in an array and alert a random entry of it.

let successMsg = ['Correct', 'Cool'];
let errorMsg = ['Wrong', 'false'];

alert(successMsg[Math.floor(Math.random() * successMsg.length)]);
function ClickAnswer() {
    if (answer == document.getElementById("outputVal").value) {
        alert(successMsg[Math.floor(Math.random() * successMsg.length)]);

       location.reload();

    } else {
        alert(errorMsg[Math.floor(Math.random() * successMsg.length)]);
    }
baao
  • 71,625
  • 17
  • 143
  • 203
0

The issue is that you are writing all the message at the same time like this :

   window.alert("Correct");
   window.alert("Yes!");
   window.alert("Well done");

Instead you may store the messages into and array, pick up a random number and select a message from this array. You may try something like this :

var message = ["Correct","Yes!","Well done"];

var a = Math.floor(Math.random() * message.length);

window.alert(message[a]);
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

What you're looking to do is make use of Math.random() just like you did for the number selection. Assign each of the correct answers to one array, and each of the incorrect answers to another array. Then you can retrieve them with:

correct_answers[Math.floor(Math.random()*correct_answers.length)];
imcorrect_answers[Math.floor(Math.random()*incorrect_answers.length)];

I've created a stripped-down version of your original code showcasing this here:

var correct_answers = ["Correct", "Yes!", "Well done"];
var incorrecty_answers = ["No, try again.", "try again", "wrong"];

correct_answers[Math.floor(Math.random()*correct_answers.length)];

function ClickAnswer() {
  if (true) {
    window.alert(correct_answers[Math.floor(Math.random()*correct_answers.length)]);
  } else {
    window.alert(incorrect_answers[Math.floor(Math.random()*incorrect_answers.length)]);
  }
}

ClickAnswer();

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
0

One way that you could go is to store the String values that you want to randomize in an array and randomly choose and return a specific index from that. Take a look here.

In your case it could be

var positiveAnswers= ['Correct!', 'Yes!', 'Well done!'];    
var randomAnswer = positiveAnswers[Math.floor(Math.random() * positiveAnswers.length)];

window.alert(randomAswer);
Michael Birsak
  • 194
  • 2
  • 12