从数组中拼接对象——未定义的错误

时间:2022-10-06 21:29:53

Within the code below contains a very simple question game (work-in-progress). So far, I've been able to have a "Rules" message appear through the onLoad function, "qDisplayLoad()". Next, the onClick function "qDisplayClick" runs anytime someone clicks, thus randomly pulling from the array (qArray).

在下面的代码中包含一个非常简单的问题游戏(正在进行中的工作)。到目前为止,我已经能够通过onLoad函数“qDisplayLoad()”显示一条“规则”消息。接下来,onClick函数“qDisplayClick”将在用户单击时运行,从而从数组(qArray)中随机抽取。

My question: How do you remove (splice) the question chosen from the array once it has been shown? My goal is to not have the same question repeat twice. Within the code below, you will find my amateur attempt at "splice" (and it kind of works), but instead, the array pushes out "undefined".

我的问题是:在数组显示后,如何从数组中删除(拼接)所选择的问题?我的目标是不让同样的问题重复两次。在下面的代码中,您将会发现我的业余尝试在“splice”(它是一种工作),但相反,该数组推出“未定义”。

var qLoad = 0;
var qArray = [
    "Question 1",
    "Question 2",
    "Question 3",
    "Question 4",
    "Question 5"
];


var randomElement;

/* The text "Rules" appears when the page loads */
function qDisplayLoad() {

    if (qLoad == 0) {
        randomElement = "Rules";
        qLoad = 1; /* Changes qLoad to "1" from "0" so the array begins within the "qDisplayClick" function */
    }

    document.getElementById("questions").innerHTML = randomElement;
}

/* A new question is pulled from the array everytime the mouse is clicked (up to a maximum of 5) */
function qDisplayClick() {
    var randomNumber = Math.floor(Math.random() * qArray.length); /* Questions are randomly chosen and rounded down from the array */

    if (qLoad += 1 && !(qLoad == 7)) {
        qArray.splice(qArray,1);
        randomElement = qArray[randomNumber];

    } else if (qLoad == 0) {
        randomElement = "Rules";
    }
    if (qLoad == 7) { /* After the 5th question, text will appear for the pop up */
        randomElement = "WIP - POP UP PLACEHOLDER";
    }

    document.getElementById("questions").innerHTML = randomElement;
}

2 个解决方案

#1


5  

The error comes from this line qArray.splice(qArray,1); The splice method can't take an array as the first parameter:

这个错误来自于这个行qArray.splice(qArray,1);splice方法不能将数组作为第一个参数:

array.splice(index, howmany, item1, ....., itemX)
  • index Required.
  • 需要索引。
  • howmany Optional.
  • 多少可选的。
  • item1, ..., itemX Optional. The new item(s) to be added to the array
  • item1,……,itemX可选的。要添加到数组中的新项

#2


1  

Thank you very much, Melchia.

非常感谢你,梅尔西亚。

Yes, you are correct! I was able to resolve this issue by updating the following:

是的,你是正确的!我可以通过更新以下内容来解决这个问题:

randomElement = qArray[randomNumber];
qArray.splice(randomNumber,1);

Thank you, and happy new year!

谢谢大家,新年快乐!

#1


5  

The error comes from this line qArray.splice(qArray,1); The splice method can't take an array as the first parameter:

这个错误来自于这个行qArray.splice(qArray,1);splice方法不能将数组作为第一个参数:

array.splice(index, howmany, item1, ....., itemX)
  • index Required.
  • 需要索引。
  • howmany Optional.
  • 多少可选的。
  • item1, ..., itemX Optional. The new item(s) to be added to the array
  • item1,……,itemX可选的。要添加到数组中的新项

#2


1  

Thank you very much, Melchia.

非常感谢你,梅尔西亚。

Yes, you are correct! I was able to resolve this issue by updating the following:

是的,你是正确的!我可以通过更新以下内容来解决这个问题:

randomElement = qArray[randomNumber];
qArray.splice(randomNumber,1);

Thank you, and happy new year!

谢谢大家,新年快乐!