www.webdeveloper.com
Results 1 to 6 of 6

Thread: Why is this function returning false?

  1. #1
    Join Date
    Jul 2011
    Posts
    3

    Post Why is this function returning false?

    So for whatever reason the convertToArray function in the following code returns false if the argument is more than 1 character long. If it's 1 character long it just returns the character as an array with only one value.

    What I'm trying to do is take a string of numbers, plus signs, and minus signs and convert it to an array that looks like this:

    Original String: 3+2-1+-2
    Returned Array: 3, 2, -1, -2

    And there's probably an easier way to do this so feel free to let me know that as well haha. Anyways, here's the code:
    Code:
    		function mergeValue(_sign, _value)
    		{
    			var _merged = _sign + _value;
    			
    			if(_sign == "+")
    			{
    				return _value;
    			}
    			else
    			{
    				return _merged;
    			}
    		}
    		function convertToArray(string)
    		{
    			if(!isValid(string))
    			{
    				//return false;
    			}
    			
    			var newVal = new Array();
    			var i;
    			var sign = "+";
    			var value = "";
    			var count = 0;
    			
    			for(i = 0; i < string.length; i++)
    			{
    				a = string.charAt(i - 1);
    				b = string.charAt(i);
    				
    				if(!isNaN(b))			//IF B IS A NUMBER
    				{
    					value = value + b;
    				}
    				else if(b == "+")		//IF B IS A +
    				{
    					if(!isNaN(a))
    					{
    						newVal[count] = mergeValue(sign, value);
    						sign = "+";
    						count = count + 1;
    						value = "";
    					}
    				}
    				else if(b == "-")		//IF B IS A -
    				{
    					if(!isNaN(a))		// -- IF A IS A NUMBER
    					{
    						newVal[count] = mergeValue(sign, value);
    						sign = "-";
    						count = count + 1;
    						value = "";
    					}
    					else if(sign == "+") {sign = "-";}
    					else if(sign == "-") {sign = "+";}
    				}
    				
    				if(i == (string.length - 1))
    				{
    					newVal[count] = mergeValue(sign, value);
    				}
    				
    				return newVal;
    			}

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Sorry I can't directly answer the 'why' as I did not feel like debugging that code, as:
    And there's probably an easier way to do this so feel free to let me know that as well haha
    Yah, time for the power of regular expressions, no doubt:
    Code:
    //string may contain signed integers and/or floats
    function convertToArray(string) {
          //match negative '-' sign if present, followed by digits and/or a dot '.' if present, then digits
        var nArr = string.match(/-?\d*\.?\d+/g);
        if (nArr !== null) { //convert all to Numbers instead of strings
            for (var i = 0; i < nArr.length; i++) {
                nArr[i] = Number(nArr[i]);
            }
        }
        return nArr;
    }
     
    var arr = convertToArray('3+2-1+-2+0.45-.66');
    alert(arr.join('\n'));

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    a modern way:
    Code:
    function convertToArray(string) {
      return  string.split(/(\D\d+)/).filter(String).filter(isFinite).map(Number);
    }
    Last edited by rnd me; 08-15-2011 at 06:36 PM.

  4. #4
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Don't forget to include your definitions of filter and map prototype methods for backwards browser compatibility....

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Apologies, rnd_me, for the tongue out, was just being playful and hope you did not take offense. Just noticed looking at this again and even though the o.p. has not requested parsing of floats within string, I felt it was a worthy addition, and your regular expression of /(\D\d+)/ fails it miserably if string were something like:
    Code:
    '123.45+3+2-1+-2+0.45-5.66'
    so the correct expression to use in your version of the convertToArray function would be:
    Code:
    /(-?\d*\.?\d+)/

  6. #6
    Join Date
    Jul 2011
    Posts
    3

    Arrow

    Thanks for the help fellas, but I don't really understand the code and I was hoping to add other supported characters like parentheses and exponents and stuff eventually. Can someone please link me to somewhere that explains the syntax of using arguments like this?
    Code:
    /(-?\d*\.?\d+)/

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