1

Trying to create a word scrambler which takes text from a text box, scrambles the letters, and repeats them in a different text box. Code must use a loop to distribute the text across an array of letters. Once I reach the Math.floor Object, I'm confused as to how to proceed.

Relevant Code:

    <script type="text/javascript">

    var word = document.getElementById("input").value;
    var wordLength = word.length;
    var scrambled = "";

    for (var i = 0; i < wordlength; i++) {
        var charIndex = Math.floor(Math.random() * word.length);
        scrambled += word.charAt(charIndex);
        word = word.substr(0, charIndex) + word.substr(charIndex + 1);
    }

    document.getElementById("output").value = scrambled;

    }

    </script>

    <head>
    <body>
    <form>  
    <input type="text" name="input" id="input" value="" maxlength="10"> <input type="text" name="output" id="output" value="" disabled="true"><br/>
    <input type="button" name="generate" value="Generate" onClick="Scramble(this.form)">
    </form>
    </body>
    </html>
TimWolla
  • 31,849
  • 8
  • 63
  • 96
Kevin56689
  • 13
  • 1
  • 5
  • 3
    If all you're looking to do is randomize the characters array, see [How to randomize a javascript array?](http://stackoverflow.com/questions/2450954/how-to-randomize-a-javascript-array). Also, avoid variable names like `random`, as those may be confusing or even reserved words. – Boaz Feb 18 '13 at 21:17
  • 1
    This seems like a homework question. Is that the case? – Jordan Dea-Mattson Feb 18 '13 at 21:19
  • Yes. This is for my high school computer sciences class. – Kevin56689 Feb 18 '13 at 21:22

2 Answers2

0

A simple way to randomise the word is as follows:

var word = "hello";
var wordLength = word.length;
var scrambled = "";

for (var i = 0; i < wordLength; i++) {
    var charIndex = Math.floor(Math.random() * word.length);
    scrambled += word.charAt(charIndex);
    word = word.substr(0, charIndex) + word.substr(charIndex + 1);
}

What this does is remove one letter from the word at each loop and append it to a string which becomes your randomised word. Math.random generates a random number between 0 and 1, and Math.floor rounds that result down. Because there are six letters in the word to start, this produces a number between zero and five (which is what you want as there is no sixth index).

All there is left to do is pop the result into a text box.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • that solution only partly works. If I enter a word "happy" the code only returns two letters instead of five. (pp, hp, ap etc.) I replace "hello" with my textbox value, and at the end I made my output textbox value equal to word. – Kevin56689 Feb 18 '13 at 21:40
  • The generated word is not the same length as the one I've put in. I will update my code so you can see what I've done. – Kevin56689 Feb 18 '13 at 21:44
  • Look at your code. This is homework so you should try to understand what you are doing instead of blindly copying ;). `scrambled` holds the output, not `word`. And your code is not the same as mine (second argument in `for` parentheses. Yes, case matters.) – Levi Botelho Feb 18 '13 at 21:48
  • I've been trying to understand what I'm doing -- trust me! :) I've been trying to do this for so long that I think my brain has finally decided "I give up." Thank you for the help. – Kevin56689 Feb 18 '13 at 21:52
  • No worries. Did this answer your question or do you still need more help? – Levi Botelho Feb 18 '13 at 21:54
  • I got it to work!!! Many thanks. I'm so happy you have NO FRICKEN IDEA. I have seriously been working on this for 2 days. You are a god among men and I bow to you!! – Kevin56689 Feb 18 '13 at 21:57
0

This loop measures the shrinking original while randomly removing letters for the scrambled one-

function scrambleWord(s){
    var word= s.value.split(''), scram= '';
    while(word.length){
        scram+= word.splice(Math.floor(Math.random()*word.length), 1)[0];
    }
    return scram;
}

alert(scrambleWord({value:'spinach'}));

kennebec
  • 102,654
  • 32
  • 106
  • 127