简体   繁体   中英

Randomly generating a word with an existing word

What I have to do

Create a program that asks a user for a word of any length. Then randomly generate a new word of the same length using only the letters of the first word. Use any letter any number of times. Ensure you capitalize only the first letter of the new word. NOTE. If there is a space in the user word, then you'll have a space in your output. Capitalize each word. Example:

User Word : Dogma Randomly generated word might be Gogoa or Oogma or Gmado notice, only in the last example did I actually use each letter exactly once. You must: Use an array, a loop, conditional, function (other than onclick), your code must start with Onclick. Use all BP's.
Algorithm:

determine the length of the word.

  • slice the word into an array of letters…. use a loop
  • randomly generate a number between 1 and the length of the word
  • assemble the new word, capitalize the first letter, ensure others are lowercase.
  • repeat 3 and 4
  • display new word.

(me)

So I have a text box and i used word length to get the amount of letters in that word. Then I have a repeating for loop to get the array from the word and I made a random number. How would I turn this stiff into letters and scramble it? I know it isn't nearly done I just want some tips.

<html>
    <head>
        <title>Final</title>
    </head>
    <body>
        <h1>Final</h1>

        Random Word generator <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(start = 0; start < n; start++){
                    Array.from(word)
                    do{
                        var x = Math.round((Math.random() * n) + 1);
                        function capitalize(str) {
                            return str.replace(/\w\S*/g, function(txt) {
                                return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
                            });
                        }
                    }
                    while{
                     x != n;
                    }
                document.getElementById("word").value = (capitalize(word));
            }
        </script>
    </body>
</html>

Once you have your string, you can use split on the string to get an array.

From here you can shuffle the array with something like: Shuffle array Now you want to grab the first element and uppercase it. Then you can use Array.join('') to get your new string. Array.join takes a value for seperator, which in your case you want to be '' so you don't end up with a string looking like H,e,l,l,o .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM