选取随机数组元素,将其移除,并在没有更多可用元素时显示消息

Pick random array element, remove it, display a message when no more elements available

本文关键字:消息 显示 元素 数组元素 随机 选取      更新时间:2023-09-26

好吧,所以我正在制作一个网站,当用户按下按钮时,它会生成一个随机的自残选项。我使用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); 
}

这里有一个简单的函数,可以在数组中循环,随机选择和删除元素,希望这对你有帮助?