简体   繁体   English

将数组拆分为唯一字符或检查字符是否存在

[英]split array into unique characters or check if character exists

I've created 3 buttons with different names, when button A is clicked it appends the value to an array. 我创建了3个名称不同的按钮,单击按钮A会将其值附加到数组中。 for an example 举个例子

<a href="#" class="answer" data-value="Football">Football</a>

clicking that will add "Football" to a blank array I'm creating a basic hangman game and want to know how I would go about either clicking a letter and checking if that letter exists within the array (ie clicking f and then checking whether f exists in the word football), or splitting the array into individual characters (so football becomes f, o, o, t, b, a, l, l) and then clicking a letter will check if that letter exists within the array. 单击该按钮将“ Football”添加到空白数组中。我正在创建一个基本的子手游戏,想知道如何单击一个字母并检查该字母是否存在于数组中(例如,单击f然后检查f或将数组拆分成单个字符(足球变成f,o,o,t,b,a,l,l),然后单击一个字母将检查该字母是否在数组中。

Hopefully that made sense as im quite new to this, so any help would be appreicated 希望这对我来说是很新的,所以任何帮助将不胜感激

You can use split to create an array of characters, eg 您可以使用split创建一个字符数组,例如

var guessWord = "Football";
var checkArray = guessWord.split("");

check to see if characters is in array: 检查字符是否在数组中:

checkArray.indexOf("o")!=-1

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

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