将一个单词拼写成随机字母

时间:2017-02-20 20:06:53

标签: javascript html arrays string

该程序应该将您输入的单词放入一个框中,将其分成一个字母数组并随机播放。之后,它将第一个字母大写,并将其余部分小写,并将其输出到同一个框中。

我试图将一个单词拼写成随机字母,但我无法通过此错误。

在Chrome中,它说我有一个意想不到的标识符,在mozilla中它说我错过了newWord = shuffle(newWord)的括号; *固定

编辑:现在我有一个错误,说大写不是一个函数。

<html>

<head>
<title>Final</title>
</head>

<body>
<h1>Final</h1> Random Word scrambler
<br>
<input type="text" id="word">
<input type="button" id="submit" value="Randomize" onclick="Random()">
<script language="javascript">
    word = document.getElementById("word").value
    var n = word.length;

    function Random() {
            for (var start = 0; start < n; start++) {
                var newWord = word.charAT(start)

                    newWord = shuffle(newWord);

                    function shuffle(array) {
                        var currentIndex = array.length,
                            temporaryValue, randomIndex;

                        while (0 !== currentIndex) {

                            randomIndex = Math.floor(Math.random() * currentIndex);
                            currentIndex -= 1;

                            temporaryValue = array[currentIndex];
                            array[currentIndex] = array[randomIndex];
                            array[randomIndex] = temporaryValue;
                        }
                        return array;
                    }

                    function capitalize(str) {
                        return str.replace(/\w\S*/g, function(txt) {
                            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
                        });
                   Array.join(newWord);
                    }
                }
                if (newWord == newWord){

                  document.getElementById("word").value = (capitalize(newWord));
                }
            }

    </script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

这一行:

var newWord = (word.charAT(start)

应该是:

var newWord = word.charAt(start)

而且,这是您尝试做的更简化的版本:

&#13;
&#13;
// Get a reference to the DOM element, not a property of the element
// that way you can access another property later without having
// to re-scan the DOM for the element again
var txtWord = document.getElementById("word");
var btn = document.getElementById("submit");

// Set up your events using DOM standards and not inline HTML event attributes
btn.addEventListener("click", random);

// By convention, use camelCase for naming
function random() {

  // Separate all the letters into an array
  var letterArray = txtWord.value.split("");
  
  // create an new array from scrambled letters in the original array
  var scrambledArray = [];
  
  var len = letterArray.length;
  for (var start = 0; start < len; start++) {
    // Get a random number between 0 and the highest array index and put it in the new array
    var num = Math.floor(Math.random() * letterArray.length)
    scrambledArray.push(letterArray[num]);
    
    // Remove letter from original array
    letterArray.splice(num,1);
  }
  console.log(scrambledArray.join(""));
}
&#13;
<h1>Final</h1> Random Word scrambler
<br>
<input type="text" id="word">
<input type="button" id="submit" value="Randomize">
&#13;
&#13;
&#13;