gpt4 book ai didi

javascript - 如果我得到需要的数组元素返回消息

转载 作者:行者123 更新时间:2023-11-29 21:10:39 25 4
gpt4 key购买 nike

我的 codepen .如果我从数组中获得所需的元素,我将尝试输出一条消息。

JavaScript:

function GetValue()
{
var myarray = new Array("word1","word2","word3","word4","word5");
var random = myarray[Math.floor(Math.random() * myarray.length)];
document.getElementById("message").innerHTML=random;
};
//rand array element

var clicks = 0;
function onClick() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
};
//count clicks

function printMessage(random, clicks) {
if (random === "word1") {
document.getElementById("alert").innerHTML =
"you get " + random + "after " + clicks + "clicks";
}
}
//display "you get word1 after _ clicks"

最佳答案

您应该在onClick 方法中调用printMessage 函数。

function onClick() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
printMessage(document.getElementById("message").innerHTML,clicks);
};

解决方法:

function GetValue()
{
var myarray = new Array("word1","word2","word3","word4","word5");
var random = myarray[Math.floor(Math.random() * myarray.length)];
document.getElementById("message").innerHTML=random;
}; //rand array element

var clicks = 0;
function onClick() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
printMessage(document.getElementById("message").innerHTML,clicks);
}; //count clicks

function printMessage(random, clicks) {
if (random === "word1") {
document.getElementById("alert").innerHTML =
"you get " + random + " after " + clicks + " clicks";
}
} //display "you get word1 after _ clicks"
<input type="button" id="btnSearch" value="Search" onclick="GetValue();onClick()" />
<p id="message"></p>

<p>Clicks: <a id="clicks">0</a></p>

<p id="alert"></p>

关于javascript - 如果我得到需要的数组元素返回消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com