替换数组Javascript中多个索引处的内容 [英] Replace content at multiple indexes in an array Javascript

查看:46
本文介绍了替换数组Javascript中多个索引处的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript制作"Hangman"游戏,在该游戏中,用户输入一个单词供其他人猜测.该单词被发送到一个函数(makeUnderlines),并使其成为下划线数组,并返回给播放器,并提示输入他们想猜的字母.字母和单词将发送到一个函数,该函数查找单词中字母的位置(getPosition),然后将位置发送给另一个函数.假定函数(insertLetters)在给定位置用字母替换了下划线.除了函数不会替换数组中的任何内容外,所有其他工作均有效.如果单词中只出现一次,则仅替换第一个字母.任何解决此问题的建议将不胜感激.我也想知道是否有一种方法可以防止用户用单词或猜测来输入数字.

I'm trying to make a Hangman game with Javascript, where the user enters a word for another person to guess. The word is sent to a function (makeUnderlines) and is made into an array of underlines and returned to the player with a prompt to enter a letter that they want to guess. The letter and the word is sent to a function that finds the positions of the letters (getPosition) in the word and sends the positions to another function. The function (insertLetters) is supposed replace the underlines with the letter at the positions given. All of this works except that the function doesn't replace anything in the array. It only replaces the first letter if it only occurs once in the word. Any advice to solving this would be greatly appreciated. I would also like to know if there is a way to prevent the user to entering a number in a word or as a guess.

<!DOCTYPE HTML>

<HTML>
  <HEAD>
    <META CHARSET="UTF-8">
    <TITLE>Hangman</TITLE>
  </HEAD>
  <BODY>
    <SCRIPT TYPE="text/javascript">

      function makeUnderlines(word) {
        for(i = 0; i < word.length; i++){
          underlines.push("\"_\""); 
        }
        return underlines;
      }

      function getPositions(word, letter) {
        for(var i=0; i< word.length;i++) {
        if (word[i] === letter) positions.push(i);
        }
        insertLetters(underlines, positions, letter)
      }

      function insertLetters(underlines, positions, bokstav){
        underlines[positions] = letter;
        return underlines;
      }

      let word = prompt("Choose a word for the player!");
      underlines = [];
      positions = [];
      makeUnderlines(word);
      
      for(i = 7; i > 0; i--){
        letter = prompt("["+underlines+"]\n Guess a letter. You have " + i + " guesses left");
        getPositions(word, letter);
      }

    </SCRIPT>
  </BODY>
</HTML>

推荐答案

一种开始研究此问题的方法是使用调试器跟踪应用程序的流程. Chrome开发工具具有调试器,可让您逐行逐步查看您变量的当前值是.

One way to start digging into this is by tracing the flow of the application with a debugger. Chrome Dev Tools has a debugger that lets you step line by line to see what the current values of your variables are.

      function insertLetters(underlines, positions, bokstav){
        underlines[positions] = letter;
        return underlines;
      }

从查看 insertLetters 来看, positions 应该是整数索引值的数组,但是它直接用作整数.也许尝试遍历 positions 中的值,然后使用这些值索引下划线.

From looking at insertLetters, positions should be an array of integer index values but it is being directly used as an integer. Maybe try iterating through the values in positions and using those to index underlines.

      function insertLetters(underlines, positions, letter){
        for (let i = 0; i < positions.length; i++) {
          const pos = positions[i];
          underlines[pos] = letter;
        }
      }

我认为您可以在 getPositions 中做的最后一件事是用 positions = []; 清除positions数组.

And I think the last thing you could do in getPositions is to clear out the positions array with positions = [];.

总体来说,您的代码看起来很实用.小心尝试将变量直接传递给函数,而不要依赖于全局变量.

Overall your code looks quite functional. Take care to try and pass variables to functions directly instead of relying on global variables.

这篇关于替换数组Javascript中多个索引处的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆