www.webdeveloper.com
Results 1 to 3 of 3

Thread: Split Array String

  1. #1
    Join Date
    Jun 2014
    Posts
    1

    Split Array String

    Hi All,

    This is my first post, probably of many as I learn javascript. I'm trying to teach myself using codeschool / code academy and I'm having issues on one very large concept -- functions. I understand the premise, but I've never used them before in the applications I am good with, like excel / crystal reports. Anywho, I was trying to build a simple function which takes the zero position in an array and splits it up. The first position in the array is a full name (first name last name). The function should split this using space as a deliminator. This is what I have so far

    Code:
    var array = ["Leeroy Jenkins", "WhelpLover"];
    function splitName (newArray){
    newArray = array[0].split (" ");
    }

    So when I run this through jsconsole, the return is "undefined". If I remove the function aspects of it, and only use

    Code:
    var array = ["Leeroy Jenkins", "WhelpLover"];
    newArray = array[0].split (" ");
    newArray;
    the return I get is correctly ["Leeroy","Jenkins"]. Can anyone provide insight on what I'm doing wrong with the function? Really appreciate it!

    Thanks in advance,
    Grij

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    Question

    I'm not sure what your display code looks like, but with a change to an alert the script seems to work fine.

    Code:
    <script type="text/javascript">
    var array = ["Leeroy Jenkins", "WhelpLover"];
    function splitName (newArray){
      var newArray = array[0].split (" ");
      alert (newArray);
    }
    splitName(array);
    </script>

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,223
    Not sure what it is you want to know but...

    Code:
    function splitName (newArray){
    newArray = array[0].split (" ");
    }
    Your function needs to have the data returned from it to be of any use and it also needs to address variables properly.

    Code:
    function splitName (srcArray){
        newArray = srcArray.split (" ");
        return newArray;
    }
    so when you invoke the function...


    newArray = splitName ( array[0] );

    your result will be ["Leeroy","Jenkins"]
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

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

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