简体   繁体   English

在window.alert(Javascript)中分配随机消息

[英]Assign Random Messages in window.alert (Javascript)

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. 我是一名数位艺术专业的学生,​​我在大学一年级,最近我们接到了建立一个基本的E-Learning数学学习网站的任务。 Basically you're asked what (for example) 7 times 7 is, you enter the answer and click a button, an alert window comes up. 基本上,系统会询问您(例如)7乘7是什么,您输入答案并单击一个按钮,将出现一个警报窗口。 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>

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)]);
    }

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. 相反,您可以将消息存储到and数组中,选择一个随机数,然后从该数组中选择一条消息。 You may try something like this : 您可以尝试这样的事情:

 var message = ["Correct","Yes!","Well done"]; var a = Math.floor(Math.random() * message.length); window.alert(message[a]); 

What you're looking to do is make use of Math.random() just like you did for the number selection. 您想要做的就是像使用数字选择一样使用Math.random() 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! 希望这可以帮助! :) :)

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. 一种可行的方法是将要随机化的String值存储在数组中,然后从中随机选择并返回特定的索引。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM