选取随机数组元素,将其移除,并在没有更多可用元素时显示消息
Pick random array element, remove it, display a message when no more elements available
好吧,所以我正在制作一个网站,当用户按下按钮时,它会生成一个随机的自残选项。我使用JQuery.load()方法来显示一个数组元素,但在选择该元素后,我似乎找不到从数组中删除该元素的方法。我还想显示一条消息,通知用户一旦没有更多的替代显示。
我尝试过.spicce()和.shift()方法,但它似乎仍然显示重复。我可能只是没有正确使用这些。
任何帮助都将不胜感激。
var skillList = new Array;
skillList[0] = "Math.floor(Math.random() * skillListAnxious.length)";
skillList[1] = "Math.floor(Math.random() * skillListAngry.length)";
skillList[3] = "Math.floor(Math.random() * skillListSad.length)";
skillList[3] = "Math.floor(Math.random() * skillListDistract.length)";
skillList[4] = "Math.floor(Math.random() * skillListThink.length)";
skillList[5] = "Math.floor(Math.random() * skillListSensation.length)";
skillList[6] = "Math.floor(Math.random() * skillListIllusion.length)";
skillList[7] = "Math.floor(Math.random() * skillListBizzare.length)";
var skillListAnxious = new Array;
var skillListSad = new Array;
var skillListAngry = new Array;
skillListAngry[0] = "skills.html #goForRun";
skillListAngry[1] = "skills.html #pillowFightWall";
skillListAngry[2] = "skills.html #breakSticks";
skillListAngry[3] = "skills.html #popBalloon";
skillListAngry[4] = "skills.html #calmJar";
skillListAngry[5] = "skills.html #scribbleonPeople";
skillListAngry[6] = "skills.html #stabFruit";
skillListAngry[7] = "skills.html #appleAgainstWall";
skillListAngry[8] = "skills.html #screamLoudly";
skillListAngry[9] = "skills.html #tearPaperObjects";
skillListAngry[10] = "skills.html #gym";
skillListAngry[11] = "skills.html #singtomusic";
skillListAngry[12] = "skills.html #pictureofangryness";
skillListAngry[13] = "skills.html #beatUpaStuffedBear";
skillListAngry[14] = "skills.html #bubblewrap";
skillListAngry[15] = "skills.html #popMultipleBalloons";
skillListAngry[16] = "skills.html #paperFillCrosshatches";
skillListAngry[17] = "skills.html #goForRun";
skillListAngry[18] = "skills.html #colourWholePaper";
skillListAngry[19] = "skills.html #throwIce";
skillListAngry[20] = "skills.html #cutSomethingElse";
skillListAngry[20] = "skills.html #cutSomethingElse";
skillListAngry[21] = "skills.html #writeDownandRip";
skillListAngry[22] = "skills.html #flattenCans";
skillListAngry[23] = "skills.html #heavyShoes";
skillListAngry[24] = "skills.html #tennis";
skillListAngry[25] = "skills.html #cutFruit";
skillListAngry[26] = "skills.html #markPicture";
skillListAngry[27] = "skills.html #slashBottle";
skillListAngry[28] = "skills.html #destroyPillows";
skillListAngry[29] = "skills.html #splatterPaint";
skillListAngry[30] = "skills.html #brushTeddyVicious";
skillListAngry[31] = "skills.html #watchFilm";
var skillListDistract = new Array;
skillListDistract[0] = "skills.html #cupTea";
skillListDistract[1] = "skills.html #hotBath";
skillListDistract[2] = "skills.html #mopBlood";
var skillListThink = new Array;
skillListThink[0] = "skills.html #butterflyProject";
skillListThink[1] = "skills.html #sample"
var skillListSensation = new Array;
var skillListIllusion = new Array;
var skillListBizzare = new Array;
function pick() {
var skillChoice = Math.floor(Math.random() * skillList.length);
$(".resultContent").load(skillList[skillChoice]);
if (document.filter.anxious.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListAnxious.length);
$(".resultContent").load(skillListAnxious[skillChoice]);
}
if (document.filter.sad.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListSad.length);
$(".resultContent").load(skillListSad[skillChoice]);
}
if (document.filter.angry.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListAngry.length);
$(".resultContent").load(skillListAngry[skillChoice]);
}
if (document.filter.distract.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListDistract.length);
$(".resultContent").load(skillListDistract[skillChoice]);
}
if (document.filter.think.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListThink.length);
$(".resultContent").load(skillListThink[skillChoice]);
}
if (document.filter.sensation.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListSensation.length);
$(".resultContent").load(skillListSensation[skillChoice]);
}
if (document.filter.bizzare.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListBizzare.length);
$(".resultContent").load(skillListBizzare[skillChoice]);
}
if (document.filter.illusion.checked == true) {
var skillChoice = Math.floor(Math.random() * skillListIllusion.length);
$(".resultContent").load(skillListIllusion[skillChoice]);
}
}
JavaScript的.splice()方法正是您所需要的。只需替换您的每一个获得以下元素的呼叫:
skillListBizzare[skillChoice]
到此:
skillListBizzare.splice(skillChoice, 1)
它将返回所选元素,并立即将其从源数组中删除。
在下一行,你可以检查是否还有一些元素:
if (skillListBizzare.length === 0) {
alert('Sorry! No skills left');
}
在没有看过您的代码的情况下,我知道splice可能是您想要的方法,但我真的必须看看它是如何设置的,以建议如何配置您的代码。
//remove the 20th index of skillListAngry array.
skillListAngry.splice(20,1);
if(skillListAngry.length === 0){
alert('there are no more items to display');
}
var array = [1,2,3,4,5,6,7,8,9,10,11,12];
// Function to generate random numbers
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Loop through the array printing and splicing random index
while(array.length > 0) {
var index = getRandomInt(0, array.length-1);
console.log(array[index]);
array.splice(index, 1);
}
这里有一个简单的函数,可以在数组中循环,随机选择和删除元素,希望这对你有帮助?
相关文章:
- 如果localstorage为空,则显示欢迎消息
- 消息显示之外的淘汰验证
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- 显示服务工作者状态的消息
- 如果用户输入的长度小于最小长度,则显示错误消息
- 当我的条件为true时,显示一条弹出消息
- 为我的rails应用程序显示javascript弹出消息
- javascript文本验证不起作用.警报消息没有't显示
- 如何在asp.net按钮上显示Bootstrap Autocloseable警报消息
- Parsleyjs:如何取消默认消息显示
- 有角度的ng消息显示ng重复表单上的错误
- 为什么我的 JS 代码没有将消息显示到 HTML 页面
- 如何停止 PHP 脚本的响应消息显示为页面的唯一内容
- 错误消息显示在所有分区中
- 警报消息显示我的逻辑是正确的,但谷歌Chrome's的html输出查看器显示不同的内容
- 插件Jquery Validation的错误消息显示不正确
- 鼠标悬停消息显示在图像的某些部分
- 检查文件是否存在,根据消息显示jQuery
- Jquery错误消息显示在每个字段上
- 当错误消息显示用户名和密码不匹配时,如何通过Cognito验证用户池中的用户?