www.webdeveloper.com
Results 1 to 4 of 4

Thread: collapse string?

  1. #1
    Join Date
    Sep 2012
    Posts
    2

    collapse string?

    Hi. I have to make something to combine two words in their common letters from left to right.
    Something like this:
    $var a='103333-7'
    $var b='103363-0'
    $var c='103331-2'
    and the result should be '103300-0'
    if the character doesn't match it will be replaced with 0.
    Many thanks!

  2. #2
    Join Date
    Nov 2010
    Posts
    1,083
    hmm..., it seems there should be a fancier way to do this, but i can't think of it...

    Code:
      <script type="text/javascript">
    var a='103333-7'
    var b='103363-0'
    var c='103331-2'
    var res =""
    for (var i = 0; i < a.length; ++i) {
    if (a[i]==b[i]&&b[i]==c[i]){
    res+=a[i];
    	} else {
    res+=0;
    	}
    }	
    alert(res)
    	 </script>

  3. #3
    Join Date
    Sep 2012
    Posts
    2
    that helps! thanks.. but what if I have n variables ?

  4. #4
    Join Date
    Jul 2011
    Posts
    24
    <!doctype html>
    <script language="javascript">
    var a='103333-7'
    var b='103363-0'
    var c='103331-2'
    var d = '203331-2'
    collapse(a,b,c,d)
    collapse(a,b)
    function collapse(){
    var arg = arguments[0];
    var res = "";
    for(var i=1; i<arguments.length; i++){
    res="";
    for(var j=0; j<arguments[i].length; j++){
    if (arguments[i][j] != arg[j])
    res += "0";
    else res += arguments[i][j];
    }
    arg=res;
    }
    alert(arg)

    }

    </script>
    Last edited by DaveyErwin; 09-17-2012 at 09:35 PM.

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