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 9 of 9
  1. #1
    New Coder
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question about the split() function

    hey just wondering if you should be able to use the split() function inside user created functions eg

    function testSplit(toBeSplit){
    var tempSplit = toBeSplit.split("");
    for(a=0;a<tempSplit.length;a++){
    document.write(tempSPlit[a]+"<br />");
    }
    }

    testSplit("string to be split");

    as the function doesnt work for me and i get a message from firebug saying toBeSplit.split is not a function yet if i split the string outside the function it works fine??

    any help is much appreciated

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    (tempSplit[a]+"<br />")
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    You must pass the value of testSplit to the function.

    <p><input type = "text" name = "testSplit" value = "string to be split" onclick= "testSplit(this.value)"></p>

    <Script type = "text/javascript">
    function testSplit(toBeSplit){
    var tempSplit = toBeSplit.split("");
    for(a=0;a<tempSplit.length;a++){
    document.write(tempSplit[a]+"<br />");
    }
    }

    </script>

  • #4
    New Coder
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    this is the part of the script thts not working:

    Code:
    function rndTotal(price){ //rounds total to nearest 2 decimal place
    	var rawNum = price.split("");
    	//var rawNum = price;
    	var num2 = new Array(rawNum.length);
    	for (a=0;a<rawNum.length;a++){
    		num2[a] = new Number(rawNum[a]);
    		if(isNaN(num2[a])){
    			num2[a] = rawNum[a];
    		}
    	}
    	var i = num2.length-1;
    	if(num2[i] < 3){
    		num2[i] = 0;
    	}
    	else if(num2[i] > 2 && num2[i] < 5 ){
    		num2[i] = 5;
    	}
    	else if(num2[i] > 2 && num2[i] < 8 ){
    		num2[i] = 5;
    	}
    	else if(num2[i] > 7 && num2[i] <= 9 ){
    		num2[i] = 0;
    		num2[i-1] = num2[i-1] + 1;
    		for(a=num2.length-2;a>=0;a--){
    			if(num2[a] == "."){
    				continue;
    			}
    			else if(num2[a] == 10){
    				num2[a] = 0;
    				if(num2[a-1] == "."){
    					num2[a-2] = num2[a-2]+1;
    				}
    				else {
    					num2[a-1] = num2[a-1] + 1;
    				}
    			}
    			else {
    				break;
    			}
    		}
    	}
    	var newTotal= num2.join("");
    	return newTotal;
    	
    }
    and this function passes rndTotal the value:
    Code:
    function quote(){
    	var tempTotal = 0;
    	for(a=0;a<subTotalArray.length;a++){
    		if(subTotalArray[a] == ""){
    			continue;
    		}
    		else{
    			tempTotal += parseFloat(subTotalArray[a]);
    		}
    	}
    	tempTotal = rndTotal(tempTotal);
    	document.getElementById('subtotal').innerHTML = tempTotal;
    	
    }
    i can get rndTotal to work by its self in a seperatescript but not in the script i want to use it in.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    function rndTotal(num) {
      var k = 1;
      while(num &#37; 1 > 0) {
        k *= 10;
        num *= 10;
      }
      var m = num % 10;
      num += -m + (m <= 2
        ? 0
        : m <= 7
          ? 5
          : 10);
      return (num / k).toFixed(2);
    }
    Code:
    function quote(sta) {
      var t = 0;
      for(var i = 0; i < sta.length; ++i)
        if(sta[i] !== "")
          t += sta[i] - 0;
      t = rndTotal(t);
      document.getElementById("subtotal").innerHTML = t;
    }
    (I tend to shy away from globals, hence the sta argument. You also might want to consider using i, j, k (in order) for for-loop counter variables. They have implied meaning.)
    Last edited by Trinithis; 12-15-2007 at 01:50 AM.
    Trinithis

  • #6
    New Coder
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    lol thanks Trinithis but i dont understand your rndTotal function.....its different to anything ive seen/tried b4.

    but on the bright side i got the function to work by converting tha value being sent to the function into a string first.


    if someone could explain the code to me and it has the same final result i'll use it instead as its much shorter code lol


    also thanx for everyones help

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    First things first, the modulus operator, which returns the remainder from a division.

    So
    Code:
    10 &#37; 3 == 1
    897 % 10 == 7
    153.123 % 1 == 0.123
    Onto rndTotal. The while loop reads as follows:
    Code:
    while(num has a decimal part) {
      multiply num by 10
      multiply k by ten
    }
    (k is used to bring back the same exact decimal places the number originally had.)

    Code:
    var m = num % 10;
    // extract the one's place of the number. put it in m
    Code:
    num += -m + (m <= 2
      ? 0
      : m <= 7
        ? 5
        : 10);
    Same as
    Code:
    if(m <= 2)
      num = num - m;
    else
      if(m <= 7)
        num = num + 5 - m;
      else
        num = num + 10 - m
    1. Does the one's place end in a 2 or less? If yes, subtract m from num
    2. Does the one's place end in a 7 or less? If yes, add 5 and subtract m
    3. Otherwise add 10 and subtract m


    Now your number is formatted. At this point, if the original number were
    123.43,
    num would be 12345
    But we want it to look like 123.45
    That is where the k comes in, which happens to be 100
    num / k == 123.45
    One thing more. We need to ensure that the resulting number is a string and has 2 decimal points in the case of a number like 23.4 -- we want 23.40
    That is what Number.toFixed(2) does.
    Last edited by Trinithis; 12-15-2007 at 06:11 PM.
    Trinithis

  • #8
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    but on the bright side i got the function to work by converting tha value being sent to the function into a string first.
    It would be better if you didn't have to manually do that. This would make it automatic:

    Code:
    function rndTotal(price){ //rounds total to nearest 2 decimal place
    	var rawNum = price.toString().split("");
    	. . . 
    }
    Trinithis

  • #9
    New Coder
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Trinithis View Post
    It would be better if you didn't have to manually do that. This would make it automatic:

    Code:
    function rndTotal(price){ //rounds total to nearest 2 decimal place
    	var rawNum = price.toString().split("");
    	. . . 
    }
    yeah thats what i did though in a different function. thanx for the help and the explaination of your function. makes sense now lol


  •  

    Posting Permissions

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