Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据用户输入使用for循环正确替换JavaScript中字符串中的字符? func=function(){ val=document.getElementById('word')。值; first=val.charAt(0.toUpperCase(); rest=val.substring(1).toLowerCase(); valConvert=第一个+休息; undersc='u'; fullUndersc=''; 对于(变量i=2;i_Javascript - Fatal编程技术网

如何根据用户输入使用for循环正确替换JavaScript中字符串中的字符? func=function(){ val=document.getElementById('word')。值; first=val.charAt(0.toUpperCase(); rest=val.substring(1).toLowerCase(); valConvert=第一个+休息; undersc='u'; fullUndersc=''; 对于(变量i=2;i

如何根据用户输入使用for循环正确替换JavaScript中字符串中的字符? func=function(){ val=document.getElementById('word')。值; first=val.charAt(0.toUpperCase(); rest=val.substring(1).toLowerCase(); valConvert=第一个+休息; undersc='u'; fullUndersc=''; 对于(变量i=2;i,javascript,Javascript,外部函数接受用户的输入,将字母转换为正确的大小写(大写和小写),保留第一个和最后一个字母的原样,并将其余字符替换为_。 内部函数(guess)再次接受用户的输入(仅一个字符),并检查该字符是否存在于用户以前的输入中。这个函数可以正常工作,但我希望它能在正确的位置用输入字母替换u。简而言之,我正在尝试制作《刽子手》游戏。任何帮助都将不胜感激 func = function() { val = document.getElementById('word').value; first = va

外部函数接受用户的输入,将字母转换为正确的大小写(大写和小写),保留第一个和最后一个字母的原样,并将其余字符替换为_。 内部函数(guess)再次接受用户的输入(仅一个字符),并检查该字符是否存在于用户以前的输入中。这个函数可以正常工作,但我希望它能在正确的位置用输入字母替换u。

简而言之,我正在尝试制作《刽子手》游戏。

任何帮助都将不胜感激

func = function() {
  val = document.getElementById('word').value;
  first = val.charAt(0).toUpperCase();
  rest = val.substring(1).toLowerCase();
  valConvert = first + rest;
  undersc = '_';
  fullUndersc = '';
  for (var i = 2; i < val.length; i++) {
    fullUndersc = fullUndersc + undersc;
  }
  wordToGuess = valConvert.charAt(0) + fullUndersc + valConvert.slice(-1);
  document.getElementById('form').innerHTML = wordToGuess;
  document.getElementById('letterIn').style.display = 'block';
  guess = function() {
  var charac = document.getElementById('letter').value;
    for (var i = 1; i < val.length -1; i++) {
      if (charac == valConvert.charAt(i)) {
        wordToGuess = wordToGuess.replace(new RegExp(wordToGuess.charAt(i), 'i'), charac);
        document.getElementById('form').innerHTML = wordToGuess;
      }
    }
  }
}

您需要跟踪原始短语以及线索

<!--HTML
<form onSubmit="func(); return false" id="form">
    Type in a word below<br /><input class="" id="word" type="text" autofocus />
  </form>

<form onSubmit="guess(); return false" id="letterIn">
Enter a letter: <input class="" id="letter" type="text" size="1" maxlength="1" autofocus />

</form>
-->
var-phrase='要猜测的短语',
线索='uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu';
函数更新(猜测){
//将线索分成一个数组
线索=线索分割(“”);
//循环浏览短语中的字符
对于(var i=0;i

.

我只看到一堆文字。这要么意味着我该睡觉了,要么你的问题不太清楚。我是JavaScript新手,可能我的问题还不够清楚。我不知道如何创建一个函数,用用户输入的字符在正确的位置交换符号。我现在将添加HTML代码,以便更容易了解我的意思。网站上的人经常提供帮助,以便于他人帮助他们。好的,谢谢你,乔纳森。非常感谢。我没意识到它这么简单!这就是我要找的。再次感谢!你需要做一些验证什么的,但这是基本的想法。请随意接受这个答案。;)好的,我完全接受你的回答,但是现在不能投票(需要有15分的声誉)。我很感激。
var phrase = 'A phrase to guess',
  clue = '_ _____ __ _____';

function updateClue (guess) {
  // split the clue into an array
  clue = clue.split('');

  // loop through the characters in the phrase
  for (var i = 0; i < phrase.length; i++) {

    // if the letter in the phrase matches the guess
    if (phrase.charAt(i).toLowerCase() === guess.toLowerCase()) {

      // replace the corresponding character in the clue
      clue[i] = phrase.charAt(i);
    }
  }

  // convert the clue array back into a string
  clue = clue.join('');
  return clue;
}