www.webdeveloper.com
Results 1 to 10 of 10

Thread: generating letters & numbers via loop

  1. #1
    Join Date
    Jun 2013
    Posts
    12

    Question generating letters & numbers via loop

    Hi.

    I have a loop, but I am having difficulty in one major key area and one in a minor area:

    MAJOR
    - I need to be capable of seeing what I am printing online or in a text document (either is fine). Right now, I see the result only in the firefox console.

    MINOR
    - I would like to be capable of telling my loop exactly where to start (for example, at aaa or btk).

    This is the code I have:


    HTML Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    </head>
    
    <body>
    
    
        <div id="entire_form">
            <form name="form_name" action="#" onsubmit="return returning_form()" id="mortage_form">
                                    
                <button type="submit" id="submit_button" name="submit_button" class="submit" value="Submit for calculation">Calculate</button>				
        
            </form>
                    <div id="results">
                    </div>
    
    </body>
    </html>

    Code:
     <script>
        function returning_form(){
        
        /////////// BEGINNING OF GLOBAL VALIDATION //////////////
    
        var valid = true;
    
        ////////// FINAL VALIDATION GREEN //////////////
    
        if (valid == true){
            
        var chars = "abcdefghijklmnopqrstuvwyxz";
        
        for(var a = 0; a < chars.length; a++) {
            for(var b = 0; b < chars.length; b++) {
            for(var c = 0; c < chars.length; c++) {
            console.log(chars[a] + chars[b] + chars[c]);
            var mytable =''
                mytable += '';	
                }
            }
        }
        
                $('#results').append(mytable);
        }
        
        
        /////////////////////// RETURN FALSE! ////////////////////////
            return true;
        
        //// CLOSING THE ORIGINAL FUNCTION CALLED returning_form /////
            
        }
        
                
        </script>

  2. #2
    Join Date
    Mar 2011
    Posts
    1,136
    Code:
            console.log(chars[a] + chars[b] + chars[c]);
    is sending the output(?) to the error console. If you want it displayed somewhere else, change this line.

    Code:
            var mytable =''
                mytable += '';
    You'll have to explain what you had in mind with the code above. As it stands, it does nothing.
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    one more hack:

    Code:
    var valid = true;
    if (valid == true){...
    can be replaced with

    Code:
    if (true){...
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    With Internet explorer, it's necessary to open the console (with F12) to avoid an error if the console do not exists.
    Then a better code is :
    Code:
    if (window.console) console.log(chars[a] + chars[b] + chars[c]);
    To display the result on a page, it's possible to build a string with the result to display (without jQuery) in a textarea or a div.
    Code:
    <body>
    <div id="pg"">
        <textarea id="myTxa" rows="7"></textarea>
        <div id="myDiv"></div>
    </div>
    <script type="text/javascript">
    function returning_str(){
        var chars = "abcdefghijklmnopqrstuvwyxz";
        var myStringDiv=myStringTxa='';
        for(var a = 0; a < chars.length; a++) {
            for(var b = 0; b < chars.length; b++) {
                for(var c = 0; c < chars.length; c++) {
                    if (window.console) console.log(chars[a] + chars[b] + chars[c]);
                  myStringDiv+=', ' + chars[a] + chars[b] + chars[c];
                    myStringTxa+='\n' + chars[a] + chars[b] + chars[c]
                    }}}
       document.getElementById('myDiv').innerHTML='<p>'+myStringDiv.substr(2)+'</p>';
       document.getElementById('myTxa').value=myStringTxa.substr(1);
    }
    returning_str();
    </script>
    </body>
    Javascript is not the rigth tools to write in a user file. Then copy and pate this result in a text file or use a server side language to build a file.

    To start with btk, use chars.indexOf(b), chars.indexOf(t), chars.indexOf(k) at the beginnings of the loops.
    Then to start with a 3 length string startString use chars.indexOf(startString[0]), chars.indexOf(startString[1]), chars.indexOf(startString[2]).
    Code:
    function returning_str(startString){
        var chars = "abcdefghijklmnopqrstuvwyxz";
        var myStringDiv=myStringTxa='';
        for(var a=chars.indexOf(startString[0]); a < chars.length; a++) {
            for(var b=chars.indexOf(startString[1]); b < chars.length; b++) {
                for(var c=chars.indexOf(startString[2]); c < chars.length; c++) {
        //...
    Last edited by 007Julien; 07-05-2013 at 09:06 AM.

  5. #5
    Join Date
    Nov 2010
    Posts
    1,053
    Quote Originally Posted by Padonak View Post
    Code:
    if (true){...
    I'm trying to imagine a situation where that would return false...

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    /////////////////////// RETURN FALSE! ////////////////////////
    return true;
    I did not want to raise this issue ...

  7. #7
    Join Date
    Jun 2013
    Posts
    12
    Thanks guys, I appreciate all your support. I just realised by printing it though that it starts with three of them. I added one to make it 4 and so on. so chars a,b,c and d for example. Is there a way to start it from somewhere in particular? Like from ccc for example?


    @Julien, j'ai faite une erreur comme tu peut voir. Merci!

  8. #8
    Join Date
    Jun 2013
    Posts
    12
    Also guys, as Julien had suggested, it would be better to save it to a txt file (or other wordprocessing). Is there a way to do that? I've never had to do that before, nor have I have come across anyone doing that, but I would love to know if there is a way. The keywords on google never yield the results I am looking for :/ Any ideas or links would be appreciated.


    @rtrethewey. I just wanted to make a fake table where my results would appear. Guess that didn't work.

    @Julien, is it me or is there a mistake in this particular code?


    Code:
    <script>
        function returning_str(startString){
        var chars = "abc";
        var myStringDiv=myStringTxa='';
        for(var a=chars.indexOf(startString[0]); a < chars.length; a++) {
            for(var b=chars.indexOf(startString[1]); b < chars.length; b++) {
                for(var c=chars.indexOf(startString[2]); c < chars.length; c++) {
    	            for(var d=chars.indexOf(startString[3]); d < chars.length; d++) {
                    
                  myStringDiv+='<br>' + chars[a] + chars[b] + chars[c] + chars[d];
                    myStringTxa+='\n' + chars[a] + chars[b] + chars[c] + chars[d]
                    }}}}
       document.getElementById('myDiv').innerHTML='<p>'+myStringDiv.substr(2)+'</p>';
       document.getElementById('myTxa').value=myStringTxa.substr(1);
    }
    returning_str(startString);
    </script>

  9. #9
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Three errors in your code :

    1. The string starString is not defined. You have to call returning_str('cccc') with your example.
    2. It not possible to find more than one result with the chars="abc", Try for example chars="abcdefgh". With the whole alphabet, there is to many possibilities and the script fails.
    3. If you replace the string ', ' before each quadruplets by a '<br>' for the mySrtingDiv, you have to remove four characters (and not two) at the beginning of the whole string.


    Code:
    function returning_str(startString){
        var chars = "abcdefghijkl";
        var myStringDiv=myStringTxa='';
        for(var a=chars.indexOf(startString[0]); a < chars.length; a++) {
            for(var b=chars.indexOf(startString[1]); b < chars.length; b++) {
                for(var c=chars.indexOf(startString[2]); c < chars.length; c++) {
    	            for(var d=chars.indexOf(startString[3]); d < chars.length; d++) {
                    
                  myStringDiv+='<br>' + chars[a] + chars[b] + chars[c] + chars[d];
                    myStringTxa+='\n' + chars[a] + chars[b] + chars[c] + chars[d]
                    }}}}
       document.getElementById('myDiv').innerHTML='<p>'+myStringDiv.substr(4)+'</p>';
       document.getElementById('myTxa').value=myStringTxa.substr(1);
    }
    returning_str('cccc');
    Last edited by 007Julien; 07-06-2013 at 07:27 PM.

  10. #10
    Join Date
    Jun 2013
    Posts
    12
    Sorry for the delay. I was unavailable for some time. I see what you mean. This is why many of my attempts failed miserably. Merci Julien!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles