...

View Full Version : Question about the split() function



aussiexile
12-14-2007, 02:21 PM
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

vwphillips
12-14-2007, 05:54 PM
(tempSplit[a]+"<br />")

Philip M
12-14-2007, 05:56 PM
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>

aussiexile
12-14-2007, 10:30 PM
this is the part of the script thts not working:


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:

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.

Trinithis
12-15-2007, 01:42 AM
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);
}




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.)

aussiexile
12-15-2007, 01:20 PM
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

Trinithis
12-15-2007, 06:05 PM
First things first, the modulus operator, which returns the remainder from a division.

So

10 &#37; 3 == 1
897 % 10 == 7
153.123 % 1 == 0.123

Onto rndTotal. The while loop reads as follows:


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.)



var m = num % 10;
// extract the one's place of the number. put it in m




num += -m + (m <= 2
? 0
: m <= 7
? 5
: 10);


Same as


if(m <= 2)
num = num - m;
else
if(m <= 7)
num = num + 5 - m;
else
num = num + 10 - m



Does the one's place end in a 2 or less? If yes, subtract m from num
Does the one's place end in a 7 or less? If yes, add 5 and subtract m
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.

Trinithis
12-15-2007, 06:20 PM
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:



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

aussiexile
12-15-2007, 10:14 PM
It would be better if you didn't have to manually do that. This would make it automatic:



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum