[thelist] JavaScript: Find matching number in list

Christian Heilmann codepo8 at gmail.com
Thu May 12 08:03:53 CDT 2005


On 5/12/05, Chris Marsh <Chris.Marsh at callserve.com> wrote:
> [..]
> 
> > con=s1.options[s1.selectedIndex].value+s2.options[s2.selectedI
> > ndex].value+s3.options[s3.selectedIndex].value;
> 
> I would ensure that the values were explicitly typed as strings to avoid the
> + operator performing an addition fnuction rather than a concatenation
> function.
> 
> function MakeString(i)
> {
>   i += '';
>   return i;
> }
> 
> var con = MakeString(s1.options[s1.selectedIndex].value);
> con += MakeString(s2.options[s2.selectedIndex].value);
> con += MakeString(s3.options[s3.selectedIndex].value);
> 
> Something along those lines should work, although the code above is
> untested.

I tested my example, and values from selects seem to be strings by
default, therefore the above is a bit redundant.
You could also use the native toString() instead of using an own function
s1.options[s1.selectedIndex].value.toString()
 
> Something else to consider is that the system you describe would appear to
> limit effective functionality to select boxes of which only one may have
> more than ten elements. Consider the concatenated result '1111'. Is this
> '11'+'1'+'1', '1'+'11'+'1' or '1'+'1'+'11'? In my limited experience it has
> proven less time consuming to produce solutions that accommodate expansion
> even on such a small scale as this. Is it possible to use a separator? For
> example, '1|11|1' uniquely expresses the combination of the three values,
> and if one is performing string functions anyway there should not be much
> more overhead in including pipes.

Exactly. I'd never rely on concatenating strings, but use an object or
a associative array instead.


More information about the thelist mailing list