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 to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 (myData){
    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?

    Thanks in advance,
    Grij

  • #2
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    EDIT: Not sure why I can't edit posts... but the first code function should be

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

  • #3
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    You have to pass the parameters in a function call not create them in the function


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

    Indifference will be the downfall of mankind, but who cares?

  • #4
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Chris,

    Thanks for the reply. I tried that code in jsconsole and it returned a syntax error. I think it was the equals sign after return. I removed it, but when I ran it through jsconsole again it returned "undefined".

    Thanks,
    Brandon

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Code:
    <script type = "text/javascript">
    var array = ["Leeroy Jenkins", "WhelpLover"];
    function splitName() {
    var newArray = array[0].split (" ");
    alert (newArray);
    }
    
    splitName();
    
    </script>
    Declare newArray outside the function or omit the var if you want it to be in global scope.

    Grija - you may not edit your posts until you have made 10 posts. This is to prevent spamming.
    Last edited by Philip M; 06-04-2014 at 08:01 PM.

    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.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Code:
    var newArray = ["Leeroy Jenkins", "WhelpLover"];
    
    function splitName(arg){
      return arg.split(" ");
    }
    
    splitName(newArray[0]); // result: ["Leeroy", "Jenkins"]
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #7
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Grija View Post
    Hi Chris,

    Thanks for the reply. I tried that code in jsconsole and it returned a syntax error. I think it was the equals sign after return. I removed it, but when I ran it through jsconsole again it returned "undefined".

    Thanks,
    Brandon
    Yes it goes along with the "don't write code when hungry" post I made because you make dumb errors.


    Code:
    	<script type="text/javascript">
    		function splitName(p_Array){
    		return p_Array[0].split(' ');
    		}
    	</script>
    }
    Chris

    Indifference will be the downfall of mankind, but who cares?


  •  

    Posting Permissions

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