0
votes

The program is supposed to tank the word you entered into a box split it into an array of letters and shuffle them. After that it capitalizes the first letter and lowercase the rest and outputs that into the same box.

I am trying to scramble a word into random letters but I can't get past this error.

In chrome it said I have an unexpected Identifier and in mozilla it said i am missing a bracket for newWord = shuffle(newWord); *fixed

Edit: now i have an error saying that that capitalize is not a function.

<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
you are getting that error b/c of this line: var newWord = (word.charAT(start), you need to delete that first (hackerrdave
And, charAT should be charAt.Scott Marcus

1 Answers

1
votes

This line:

var newWord = (word.charAT(start)

should be:

var newWord = word.charAt(start)

And, here's a much more simplified version of what you are trying to do:

// 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">