passing arguments

D

dtshedd

I am having trouble with arguments in a javascript routine. What I am
trying to do is to limit the size of numbers to ten digits but make use
of the significant places if the number is less than one.

I load a box on a form with the following

form.j.value=numToString(somevaluehere)

I have used alert statements to confirm that the value is passed to the
numToString function, but when the number is further passed to the
Precision function it becomes zero. I had read that text strings are
converted to floating point automatically in Javascript, and the number
that is passed to the numToString routine is the result of math
operations.

Appreciate any advice.

TIA

Dan

function numToString(number){

// Convert numeric values to string and truncate

digits=Precision(number)

number=number.toString()

var index= number.indexOf(".",0)

number= number.substring(0,index+(10-digits))

return number

}


function Precision(XV){

// GOSUB FOR SETTING PRECISION OF FORMAT

// LOG function in Basic language is natural log, base 10 conversion
factor 2.3

// Log base 10 of number is the quantity of significant digits, ie log
100=2

if (XV= 0){

XV= 1

}

Idloc = Math.floor(Math.log(Math.abs(XV)) / 2.302585093)

if (Idloc < 3) {

Idloc = 3

}

if (Idloc > 10) {

Idloc = 10

}

return Idloc

}

function numToString(number){

// Convert numeric values to string and truncate

digits=Precision(number)

number=number.toString()

var index= number.indexOf(".",0)

number= number.substring(0,index+(10-digits))

return number

}
 
R

RobG

I am having trouble with arguments in a javascript routine. What I am
trying to do is to limit the size of numbers to ten digits but make use
of the significant places if the number is less than one.


There are lots of issues to cover here, have a look at Dr J's site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

I load a box on a form with the following

form.j.value=numToString(somevaluehere)

I have used alert statements to confirm that the value is passed to the
numToString function, but when the number is further passed to the
Precision function it becomes zero. I had read that text strings are
converted to floating point automatically in Javascript, and the number
that is passed to the numToString routine is the result of math
operations.

Appreciate any advice. [...]
function numToString(number){

// Convert numeric values to string and truncate

digits=Precision(number)

Use semi-colons to end statements, block code correctly, indent with 2
spaces and manually wrap code at about 70 characters. Allowing
auto-wrapping nearly always introduces more errors that must be found
and fixed.

Present your code so that it can be copied directly into a test
document and will work. Provide some test cases to indicate the error
and what you expected.

number=number.toString()

Another way to convert number to a string is to prepend an empty string
to it:

number = '' + number;

Once you get too many zeros in the number, toString() returns
scientific notation (in the browseres I tested at least):

(0.000006).toString() ==> 0.000006
(0.0000006).toString() ==> 6e-7

You need to account for that in your algorithm - see the link above.

var index= number.indexOf(".",0)
number= number.substring(0,index+(10-digits))

You may be truncating the scientific notation here, not the number
itself (see the link above). e.g.

0.00000061231488 ==> 6.1231488e- instead of 6.1231488e-7

return number
}


function Precision(XV){

// GOSUB FOR SETTING PRECISION OF FORMAT
// LOG function in Basic language is natural log, base 10 conversion
factor 2.3
// Log base 10 of number is the quantity of significant digits, ie log
100=2
if (XV= 0){

Here you assign the value of zero to the variable XV, it will always
evaluate to false. I think you meant:

if (XV == 0){

A simple way to catch such errors is write them so that the invariable
bit is on the left, then you'll get an error rather than silent failure
of logic:

if ( 0 = XV ){ // --> error... so hopefully you'll find and fix it.

XV= 1
}

Idloc = Math.floor(Math.log(Math.abs(XV)) / 2.302585093)

You could use 'Math.LN10' instead of 2.302... I don't know whether
that makes much difference here.
if (Idloc < 3) {
Idloc = 3
}

This part here attempts to restrict the integer part to at least 3
digits, hence the decimal part has a maximum of 7. Change 3 to 0 to
get 10 places after the decimal point.
 
R

Randy Webb

RobG said the following on 8/13/2006 11:55 PM:
There are lots of issues to cover here, have a look at Dr J's site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

<URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >

You may want to choose another name to call JRS, as "Dr. J" was a
professional basketball player in the USA as was John Stockton. The
latter, it is my understanding, is the reason he posts as Dr. John
instead of just John.

Use semi-colons to end statements

I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't. When asked to provide "typical" code
where the lack of a semicolon caused an error when the semicolon removed
it, I got one example and Richard couldn't remember the full context.
But, there are places that adding a semi-colon to the end of a line will
cause errors where the lack of that semi-colon isn't an error.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 13 Aug 2006 20:55:24 remote, seen in
news:comp.lang.javascript said:
(e-mail address removed) wrote:
number = '' + number;

Or String(number) might be more readable.

You could use 'Math.LN10' instead of 2.302... I don't know whether
that makes much difference here.

2.302585093 is inexact, by several places. There is therefore a high
risk that some number such as 999999.999999 or 1000000.000001 will
appear to be the wrong side of its power of 10. If so, the result may
be in error by a factor of 10.

The same applies with Math.LN10, to a lesser extent; consider

for (J=-15; J<16; J++)
document.write(Math.log(Math.pow(10, J))/Math.LN10, "<br>")

for which nearly half the results are inexact low.

ISTM that considerable care is needed in mixing string and arithmetic
operations in output conversion.

OP : read the newsgroup FAQ.
 
R

RobG

Randy said:
RobG said the following on 8/13/2006 11:55 PM: [...]
There are lots of issues to cover here, have a look at Dr J's site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

<URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >

You may want to choose another name to call JRS, as "Dr. J" was a
professional basketball player in the USA as was John Stockton. The
latter, it is my understanding, is the reason he posts as Dr. John
instead of just John.

I have never heard of Dr. J the basketball player - I don't follow
American basketball and see no reason to assume the OP does - nor do I
know of a John Stockton in any other context. I assumed the OP would
get the full context by following the link.

If the link wasn't followed, the context is irrelevant.

I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't.

That is fine your brain is as efficient and rigorous as a JavaScript
interpreter, I thought the idea was that posted code should be easy to
read. It is not much of a burden to ask for semi-colons.

When asked to provide "typical" code
where the lack of a semicolon caused an error when the semicolon removed
it, I got one example and Richard couldn't remember the full context.

The topic here is posted code, I find a "full syntax" style is much
more readable even if it requires a few more characters. There are
many syntactical parts of the language that can be included or removed
without introducing errors, such as brackets and braces in various
places, but omitting them does little to aid clarity most of the time.

Omitting semi-colons makes it harder for me, and probably some others.
The fact that semi-colon insertion means they *can* be left out doesn't
mean they *should* be. It isn't an excuse to remove them completely -
you can remove the vast majority of whitespace characters too, do you
recommend that?

But, there are places that adding a semi-colon to the end of a line will
cause errors where the lack of that semi-colon isn't an error.

Because an end of line doesn't define an end of a statement - simply
adding semi-colons at the end of every line, particularly in posted
code, is almost certain to introduce errors.
 
D

dtshedd

Thanks to all who provided advice. This was only my second attempt to
use Javascript, the first was 9 years ago. I used semicolons at the
end each line then, but did not remember why. Some scripts I found on
the internet did not use them.

Again I appreciate the assistance.

Dan
Randy said:
RobG said the following on 8/13/2006 11:55 PM: [...]
There are lots of issues to cover here, have a look at Dr J's site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

<URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >

You may want to choose another name to call JRS, as "Dr. J" was a
professional basketball player in the USA as was John Stockton. The
latter, it is my understanding, is the reason he posts as Dr. John
instead of just John.

I have never heard of Dr. J the basketball player - I don't follow
American basketball and see no reason to assume the OP does - nor do I
know of a John Stockton in any other context. I assumed the OP would
get the full context by following the link.

If the link wasn't followed, the context is irrelevant.

I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't.

That is fine your brain is as efficient and rigorous as a JavaScript
interpreter, I thought the idea was that posted code should be easy to
read. It is not much of a burden to ask for semi-colons.

When asked to provide "typical" code
where the lack of a semicolon caused an error when the semicolon removed
it, I got one example and Richard couldn't remember the full context.

The topic here is posted code, I find a "full syntax" style is much
more readable even if it requires a few more characters. There are
many syntactical parts of the language that can be included or removed
without introducing errors, such as brackets and braces in various
places, but omitting them does little to aid clarity most of the time.

Omitting semi-colons makes it harder for me, and probably some others.
The fact that semi-colon insertion means they *can* be left out doesn't
mean they *should* be. It isn't an excuse to remove them completely -
you can remove the vast majority of whitespace characters too, do you
recommend that?

But, there are places that adding a semi-colon to the end of a line will
cause errors where the lack of that semi-colon isn't an error.

Because an end of line doesn't define an end of a statement - simply
adding semi-colons at the end of every line, particularly in posted
code, is almost certain to introduce errors.
 
R

Randy Webb

RobG said the following on 8/15/2006 3:40 AM:
Randy said:
RobG said the following on 8/13/2006 11:55 PM: [...]
There are lots of issues to cover here, have a look at Dr J's site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

<URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >
You may want to choose another name to call JRS, as "Dr. J" was a
professional basketball player in the USA as was John Stockton. The
latter, it is my understanding, is the reason he posts as Dr. John
instead of just John.

I have never heard of Dr. J the basketball player - I don't follow
American basketball and see no reason to assume the OP does - nor do I
know of a John Stockton in any other context. I assumed the OP would
get the full context by following the link.

If the link wasn't followed, the context is irrelevant.
I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't.

That is fine your brain is as efficient and rigorous as a JavaScript
interpreter, I thought the idea was that posted code should be easy to
read. It is not much of a burden to ask for semi-colons.

It is not about whether my brain, or yours, is as efficient or rigorous.
It has to do with the errors that can be introduced if a person doesn't
know when - and where - to insert them or leave them out. It almost
falls in the eval category where you are better off leaving them out if
you are not sure where they can or can not be.
The topic here is posted code, I find a "full syntax" style is much
more readable even if it requires a few more characters.

Are you kidding me? You are trying to tell me that this line of code:

window.alert('Your are joking!');

is "easier to read" than this line:

window.alert('Your are joking!')

Did I understand you right?

There are many syntactical parts of the language that can be included
or removed without introducing errors, such as brackets and braces in
various places, but omitting them does little to aid clarity most of
the time.

Nor does removing them remove clarity "most of the time".
Omitting semi-colons makes it harder for me, and probably some others.

See my question above. Am I understanding correctly?
The fact that semi-colon insertion means they *can* be left out doesn't
mean they *should* be. It isn't an excuse to remove them completely -
you can remove the vast majority of whitespace characters too, do you
recommend that?

Actually, yes. Just for compactness but white space actually makes code
harder to read than semicolon's missing.
Because an end of line doesn't define an end of a statement - simply
adding semi-colons at the end of every line, particularly in posted
code, is almost certain to introduce errors.

So, can you give the rules for where they should or shouldn't be?
Meaning, a simple basic set of rules on how to tell where they should or
shouldn't be. And I don't mean "At the end of a statement", I would like
an FAQ type answer that explains - to a beginner - where and how to tell
where they can or can not be inserted.
 
R

RobG

Randy said:
RobG said the following on 8/15/2006 3:40 AM:
Randy said:
RobG said the following on 8/13/2006 11:55 PM: [...]
digits=Precision(number)
Use semi-colons to end statements
I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't.

Maybe, but it will not always insert them and requires either a
smi-colon or newline to correctly interpret even simple code:

alert('hi') alert('fred')

results in a "missing ; before statement" error in Firefox.

It is not about whether my brain, or yours, is as efficient or rigorous.
It has to do with the errors that can be introduced if a person doesn't
know when - and where - to insert them or leave them out. It almost
falls in the eval category where you are better off leaving them out if
you are not sure where they can or can not be.

Anyone writing JavaScript must have some idea about where statements
start and end, and hence where to insert semi-colons - otherwise how
will they know where to insert (or not insert) new lines?

Are you kidding me? You are trying to tell me that this line of code:

window.alert('Your are joking!');

is "easier to read" than this line:

window.alert('Your are joking!')

Did I understand you right?

Yes, but that is trivial code. The context of my original request was
many lines of code.

Nor does removing them remove clarity "most of the time".

Then you probably like the chat room obsession with removing
punctuation from typed communication. i find it annoying it makes
things harder to read and hence to comprehend of course you are free to
remove punctuation if you like but then you cant get upset if someone
reinserts it where they see fit it may totally change the meaning of
what was written

See my question above. Am I understanding correctly?
Yes.



Actually, yes. Just for compactness but white space actually makes code
harder to read than semicolon's missing.

I doubt you'll find anyone who thinks removing whitespace makes code
easier to read. I agree that an excessive amount makes it harder, but
I've never seen coding standards anywhere that recommend removing it
wherever possible.

So, can you give the rules for where they should or shouldn't be?
Meaning, a simple basic set of rules on how to tell where they should or
shouldn't be. And I don't mean "At the end of a statement", I would like
an FAQ type answer that explains - to a beginner - where and how to tell
where they can or can not be inserted.

If a programmer has no idea of where a statement starts or ends, how
will they know where newlines are needed? If they know where newlines
are required (as opposed to inserted for neatness), then they probably
know where to put semi-colons too.

As for a simple explanation on where to put semi-colons, I'm not going
to take that challenge - I'll stick to "read the spec" for that one.
Maybe you can define a simple FAQ-type explanation on where to insert
new lines - "wherever you would have inserted a semi-colon" won't do
the job! :).
 
R

Randy Webb

RobG said the following on 8/17/2006 1:06 AM:
Randy said:
RobG said the following on 8/15/2006 3:40 AM:
Randy Webb wrote:
RobG said the following on 8/13/2006 11:55 PM: [...]
digits=Precision(number)
Use semi-colons to end statements
I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't.

Maybe, but it will not always insert them and requires either a
smi-colon or newline to correctly interpret even simple code:

alert('hi') alert('fred')

results in a "missing ; before statement" error in Firefox.

Yes, and it doesn't *require* a semicolon to fix the error, a line break
will do the same thing.

But, the issue of syntax errors wasn't what got this started, it was the
claim of increased readability.
Anyone writing JavaScript must have some idea about where statements
start and end, and hence where to insert semi-colons - otherwise how
will they know where to insert (or not insert) new lines?

for (var i=o;i<100;i++);

Valid "statement". But, would everybody know, at a glance whether that
semi-colon should be there or not? The answer is no. It depends on what
the next code is, and what the intentions are. That falls into the
"unless you know for sure...."

This debate can go on for years and years, so I am leaving it be. I have
had it in the past and will probably have it again in the future. And I
will repeat then what I said now and in the past:

Unless you know where to put them and where not to put them, you are
better off leaving them out.
 
D

Douglas Crockford

for (var i=o;i<100;i++);
Valid "statement". But, would everybody know, at a glance whether that
semi-colon should be there or not? The answer is no. It depends on what
the next code is, and what the intentions are. That falls into the
"unless you know for sure...."

This debate can go on for years and years, so I am leaving it be. I have
had it in the past and will probably have it again in the future. And I
will repeat then what I said now and in the past:

Unless you know where to put them and where not to put them, you are
better off leaving them out.

You make a compelling argument in favor of being ignorant and lazy. I think that
better results come from being knowledgeable and rigorous.

I wrote JSLint to automatically detect coding errors like the one you give in
your example. It will tell you where the semicolons should go.

http://www.JSLint.com/
 
R

RobG

Randy said:
RobG said the following on 8/17/2006 1:06 AM:
Randy said:
RobG said the following on 8/15/2006 3:40 AM:
Randy Webb wrote:
RobG said the following on 8/13/2006 11:55 PM: [...]
digits=Precision(number)
Use semi-colons to end statements
I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn't.

Maybe, but it will not always insert them and requires either a
smi-colon or newline to correctly interpret even simple code:

alert('hi') alert('fred')

results in a "missing ; before statement" error in Firefox.

Yes, and it doesn't *require* a semicolon to fix the error, a line break
will do the same thing.

But it proves the point that simply leaving out semi-colons is not a
remedy for ignorance of where they are required. However you look at
it, a programmer must insert either new lines or semi-colons in
appropriate places. If nothing else, using semi-colons makes a
difinitive statement on where the programmer thinks the statement ends
(or should end) and hence makes reading and debugging the code easier.

But, the issue of syntax errors wasn't what got this started, it was the
claim of increased readability.

Exactly - inserting semi-colons makes it clear where the programmer
thinks a statement ends. De-bugging code without semi-colons probably
requires a more thoorough knowledge of where they will be inserted than
inserting them in the first place.

for (var i=o;i<100;i++);

Valid "statement". But, would everybody know, at a glance whether that
semi-colon should be there or not?

Everybody? I expect that any competent programmer could answer that
based on what the next line was and the context of surrounding code -
which is pretty much the same logic whether the semi-colon is there or
not.

The answer is no. It depends on what
the next code is, and what the intentions are. That falls into the
"unless you know for sure...."

More like "unless you know at all..."

This debate can go on for years and years, so I am leaving it be. I have
had it in the past and will probably have it again in the future. And I
will repeat then what I said now and in the past:

Unless you know where to put them and where not to put them, you are
better off leaving them out.

A review of the code posted here and visible in web pages shows that an
overwhelming majority of programmers prefer to insert semi-colons where
they think they are needed. You are entitled to your opinion of
course, but it seems that everyone else is out of step but you. :)
 
R

Randy Webb

Douglas Crockford said the following on 8/20/2006 8:49 PM:
You make a compelling argument in favor of being ignorant and lazy. I
think that better results come from being knowledgeable and rigorous.

I wrote JSLint to automatically detect coding errors like the one you
give in your example. It will tell you where the semicolons should go.

"I don't have to know where they go, JSLint will tell me" and my
argument promotes ignorance and laziness?
 
R

Randy Webb

Douglas Crockford said the following on 8/21/2006 10:02 AM:
Think of JSLint as an educational tool.

I already know what JSLint is :)
In no time you will master the placement of semicolons.

I already have it mastered. It isn't my mastery of semicolons that was
the issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top