Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    31
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Array undefined problem

    Hey guys

    I got a problem with my program, it converts a name to initial

    example: Jordy Derp ==> J.D. but i always get an undefined in front of it

    http://i43.tinypic.com/2qu12jn.png

    Code:
         addEventListener("load", init, false);
    
            function init() {
    
    
                var naam = new initialen(prompt("geef een naam in"));
                var tijdelijk = ""
                var naamArray = new Array();
                var perm = ""
    
                function initialen(naam) {
    
                    if (naam.indexOf(' ') >= 0) {
                        naamArray = naam.split(" ")
    
    
                        for (var i = 0; i < naamArray.length; i++) {
                           perm += naamArray[i].charAt(0) + "."
                        }
                        alert(perm)
    
                    }
                    else {
                        alert("False")
                    }
                }
            }
    btw it also checks for spaces so u need to type 2 names


    Thanks in advance

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Declare the perm variable before you invoke the initialen() function.

    P.S. To be exact, the variable must be initialized before the function will be invoked.
    Last edited by Amphiluke; 01-05-2012 at 12:29 PM.
    I am still learning English

  • Users who have thanked Amphiluke for this post:

    jdruwe (01-05-2012)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    31
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Amphiluke View Post
    Declare the perm variable before you invoke the initialen() function.

    P.S. To be exact, the variable must be initialized before the function will be invoked.

    var perm = "" is before the function?

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Before the function invoking.
    Code:
                var perm = ""
                var naam = new initialen(prompt("geef een naam in"));
                var tijdelijk = ""
                var naamArray = new Array();
    I am still learning English

  • Users who have thanked Amphiluke for this post:

    jdruwe (01-05-2012)

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    31
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Amphiluke View Post
    Before the function invoking.
    Code:
                var perm = ""
                var naam = new initialen(prompt("geef een naam in"));
                var tijdelijk = ""
                var naamArray = new Array();
    Thanks it works now , stupid mistake ^^

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <script type = "text/javascript">
    
    var naam = prompt("Geef een naam in","");
    naam = naam.replace(/[^a-z\s]/gi,"");  // strip all but letters and spaces
    naam = naam.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    naam = naam.replace(/\s{2,}/g," ");  // replace multiple spaces with one space
    
    var perm = "";
    
    if (naam.indexOf(' ') >= 0) {
    var naamArray = naam.split(" ");
    
    for (var i = 0; i < naamArray.length; i++) {
    perm += naamArray[i].charAt(0) + "."
    }
    perm = perm.toUpperCase();
    alert (perm);
    }
    else {alert("False")}
    
    </script>

    Customer in UK travel agent shop: - "What month is it now in Australia?"

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    It seems to me that there is no need in loops at all… =)
    Code:
    var re = /\s*(\w)\S*[\s$]*/g;
    var str = "    Irvim     John  Kenneth  Loyd Martin Nero         Oliver Paul Quincy           ";
    alert(str.replace(re, "$1.").toUpperCase());
    I am still learning English


  •  

    Posting Permissions

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