SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Split String and Sum Values

    I have this function: when a user fill a field with this "c 10 20 30" i need to sum just the numbers and show the result.

    I have this but is NOT working...

    Code JavaScript:
     function escaja(str, prefix) {
        							if (str.indexOf(prefix)===0){
    							var sumar = str.split(" ");
    							var sumar1 = 0;
    							for(i = 0; i < sumar.length; i++){
     
    								if (!isFinite(sumar1)) {
    								sumar1 = Number(sumar[i]+sumar1);
    								}
    }

    Any idea...???

  2. #2
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a solution:

    Code JavaScript:
    					  function escaja(str, prefix) {
        					//return str.indexOf(prefix) === 0;
    						if (str.indexOf(prefix)===0){
    							var sumar = str.split(" ");
    							var sumar1 = 0;
     
    							for(i = 1; i < sumar.length; i++){
    								sumar1 = parseFloat(sumar[i]) + parseFloat(sumar1);
    							}
    						document.items.quantity.value = sumar1;
     
    						}
    						}

    If someone have a better idea plz let me know.


Tags for this Thread

Bookmarks

Posting Permissions

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