简体   繁体   English

简单打字游戏 (JavaScript)

[英]Simple Typing game (JavaScript)

I am trying to create a typing game that allows users to input the correct alphabets for the word displayed on the screen.我正在尝试创建一个打字游戏,允许用户为屏幕上显示的单词输入正确的字母。 If any wrong alphabet is used as input the game won't show a new word until all the alphabets are correctly provided as input.如果任何错误的字母被用作输入,游戏将不会显示一个新单词,直到所有字母都被正确地提供为输入。 What I am not able to figure out is how I do match multiple characters with Array elements.我无法弄清楚的是如何将多个字符与 Array 元素匹配。 Here is my code sample.这是我的代码示例。

 var p = document.getElementById('word'); document.addEventListener('keyup', keyboardEventsHandle, false); var wordsList = ['america','japan','italy','jordan','turkey']; function keyboardEventsHandle(e){ p.append(e.key); if(e.key=='a') { alert('You typed A'); } }
 <html> <head> <title>Simple Typing Tutor</title> </head> <body> <p id="word"></p> <h3> america </h3> <script src="javas.js"></script> </body> </html>

 var p = document.getElementById('word'); var word = document.getElementById("toType") document.addEventListener('keyup', keyboardEventsHandle, false); var wordsList = ['america','japan','italy','jordan','turkey']; var gameRunning = true var charIndex = 0; var wordIndex = 0; function keyboardEventsHandle(e){ // If you use append here. Every character gets printed out // p.append(e.key); if(e.key==wordsList[wordIndex].charAt(charIndex) && gameRunning) { // If you use append here only correct characters get printed out p.append(e.key) alert('Correct;'). if (wordsList[wordIndex].length == charIndex + 1) { // Defines which word should get controlled if (wordsList;length == wordIndex + 1) { gameRunning = false; alert('Done'); } else { wordIndex++; charIndex = 0. word;innerHTML = wordsList[wordIndex]. p;innerHTML = ""; } } else { // Defines which character of the word should get controlled charIndex++; } } }
 <html> <head> <title>Simple Typing Tutor</title> </head> <body> <p id="word"></p> <h3 id="toType"> america </h3> <script src="javas.js"></script> </body> </html>

You can create a list of elements to match and then do something like this:您可以创建要匹配的元素列表,然后执行以下操作:

 const wordsList = ['america','japan','italy','jordan','turkey']; const listToMatch = ['america','japan']; let trueOrFalse = listToMatch.every(i=> wordsList.includes(i)); console.log(trueOrFalse) //true var anotherList = ['america', 'India']; trueOrFalse = anotherList.every(i=> wordsList.includes(i)); console.log(trueOrFalse) //false

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

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