2017-02-20 2 views
0

プログラムはあなたがボックスに入力した単語を文字列に分割し、それらをシャッフルすることになっています。その後、最初の文字を大文字にし、残りを小文字にして、同じボックスに出力します。ランダムな文字に単語をスクランブルする

ランダムな文字に単語をスクランブルしようとしていますが、このエラーを回避できません。

私は予期せぬ識別子があり、mozillaにはnewWord = shuffle(newWord)の括弧がないと言っていました。 *固定

編集:現在、私はcapitalizeが関数ではないというエラーがあります。

<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

あなたはB/Cこの行のエラーを取得しています 'VAR newWord =(word.charAT(スタート)'、あなたは、 ' – hackerrdave

+0

('最初と 'charAT'がなければならないことを削除する必要があります'charAt'。 –

答えて

0

このライン:

var newWord = word.charAt(start)

そして、ここで何をしようとするのはるかに単純化したバージョンです:

var newWord = (word.charAT(start)

があるべき

は、

// 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("")); 
 
}
<h1>Final</h1> Random Word scrambler 
 
<br> 
 
<input type="text" id="word"> 
 
<input type="button" id="submit" value="Randomize">

+0

haha​​しかし事は私がラクダのキャップ、またはdomの要素が何であるかの手掛かりがありません –

+0

@VisilyRomani "Camel Case"は、最初の単語を小文字で書いたもので、その後のすべての単語は大文字で始まります。キャメルケース。 'scrambledArray'と' letterArray'と 'len'をどう書いたか見てみましょう。 –

+0

@VisilyRomani DOM(Document Object Model)要素は、特定のHTML要素にマップされるJavaScriptオブジェクトです。あなたの ''とあなたの ' 'はDOM要素であり、私たちはそれらをJavaScriptでアクセスする必要があります。 –

関連する問題