/    Sign up×
Community /Pin to ProfileBookmark

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;
}[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameAug 14.2011 — 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[/quote]
Yah, time for the power of regular expressions, no doubt:
//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 &lt; nArr.length; i++) {
nArr[i] = Number(nArr[i]);
}
}
return nArr;
}

var arr = convertToArray('3+2-1+-2+0.45-.66');
alert(arr.join('n'));
Copy linkTweet thisAlerts:
@rnd_meAug 15.2011 — a modern way:
[CODE]function convertToArray(string) {
return string.split(/(Dd+)/).filter(String).filter(isFinite).map(Number);
}

[/CODE]
Copy linkTweet thisAlerts:
@astupidnameAug 16.2011 — Don't forget to include your definitions of filter and map prototype methods for backwards browser compatibility.... :p
Copy linkTweet thisAlerts:
@astupidnameAug 16.2011 — 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 /(Dd+)/ fails it miserably if string were something like:
'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:
/(-?d*.?d+)/
?
Copy linkTweet thisAlerts:
@Registered_User2authorAug 17.2011 — 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+)/[/CODE]
×

Success!

Help @Registered_User2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...