Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12582

Re: Integer vs. string

From Tim Streater <timstreater@greenbee.net>
Newsgroups comp.lang.javascript
Subject Re: Integer vs. string
Date 2012-03-10 23:14 +0000
Organization Personal Software
Message-ID <timstreater-60ACED.23143110032012@news.individual.net> (permalink)
References <timstreater-677053.13490009032012@news.individual.net> <340a80e8-3328-4570-aee5-3e4465d0368b@kv10g2000pbc.googlegroups.com>

Show all headers | View raw


In article 
<340a80e8-3328-4570-aee5-3e4465d0368b@kv10g2000pbc.googlegroups.com>,
 RobG <rgqld@iinet.net.au> wrote:

> On Mar 9, 11:49 pm, Tim Streater <timstrea...@greenbee.net> wrote:
> > I got burnt today by doing:
> >
> >   var a = b + 1;
> >
> > Now b is supposed to be an integer, but for some reason at the time in
> > question, it was treated as a string.
> >
> > The variable b is declared globally but not initialised there. During
> > its life it is initialised to the length of an array (as in
> > myarray.length). It's operated on as follows:
> >
> > b--;
> >
> > x = b - 1;
> > // some stuff done here
> > b = x;
> 
> I think that is an obvious suspect. As Evertjan said, start looking
> for where things go wrong and see were b gets converted to a string,
> then try to isolate it as a simple test case.
> 
> Anyhow, wherever you assign a variable value to b, you could do:
> 
>   b = +x;
> 
> or more explicitly:
> 
>   b = Number(x);

Mmmm. I will do some more research on this. The variable is not actually 
called b, so it's easy to grep for and search for in the source. But I 
can do some more testing to pin it down. For now I've used parseInt at 
the one place where I do  b+1.

I was hoping it would be something obvious I'd overlooked, that some 
kind soul would say "Ah you fool Streater, doing <whatever> is bound to 
turn it into a string!".

OK - thanks for pushing me towards more debugging. I quite enjoy that.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Integer vs. string Tim Streater <timstreater@greenbee.net> - 2012-03-09 13:49 +0000
  Re: Integer vs. string "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-09 18:59 +0000
    Re: Integer vs. string Tim Streater <timstreater@greenbee.net> - 2012-03-09 21:47 +0000
  Re: Integer vs. string RobG <rgqld@iinet.net.au> - 2012-03-10 14:56 -0800
    Re: Integer vs. string Tim Streater <timstreater@greenbee.net> - 2012-03-10 23:14 +0000
  Re: Integer vs. string Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-10 21:06 +0000

csiph-web