Javsahit typing

  • Thread starter The Natural Philosopher
  • Start date
T

The Natural Philosopher

God., t took me an hour last night to add two numbers together..

Yo would have thought that the people who dreamed up this shit could
have made a slightly more sensible way of arranging typing than to
declare all variables typeless, and have a function to turn a typeless
thing into a number so that you could actually make '10'+'5' not be '105'...

Makes you long for C...
 
G

GArlington

God., t took me an hour last night to add two numbers together..

Yo would have thought that the people who dreamed up this shit could
have made a slightly more sensible way of arranging typing than to
declare all variables typeless, and have a function to turn a typeless
thing into a number so that you could actually make '10'+'5' not be '105'...

Makes you long for C...

var nVar1 = 10; // Int
var nVar2 = 5; // Int
var vcVar3 = '10'; // Varchar
var vcVar4 = '5'; // Varchar

Try adding them in different order...
 
A

Andreas M.

Am 17.10.2007 15:13 The Natural Philosopher wrote
God., t took me an hour last night to add two numbers together..

You mean like:

document.writeln(10+5);

?

Gives 15 in both IE6 and Firefox here.

And:

document.writeln("10" + "5");

gives "105".

While typeless languages ain't the holy grail, typed ones ain't either.
Maybe you should start out by RTFM ;-)
 
T

The Natural Philosopher

GArlington said:
var nVar1 = 10; // Int
var nVar2 = 5; // Int
var vcVar3 = '10'; // Varchar
var vcVar4 = '5'; // Varchar

Try adding them in different order...
But the problem is one of the variables was in fact picked up from a
Form variable..and that came up as an implicit string.

Eventually I trawled the manual to find Number()..but what a trawl!..
 
T

The Natural Philosopher

Andreas said:
Am 17.10.2007 15:13 The Natural Philosopher wrote


You mean like:

document.writeln(10+5);

?

Gives 15 in both IE6 and Firefox here.

And:

document.writeln("10" + "5");

gives "105".

While typeless languages ain't the holy grail, typed ones ain't either.
Maybe you should start out by RTFM ;-)

I did RTFM, and discovered that it is not really possible to tell what a
variable is.

Here i what I ended up with.


function bom_set()
{
var cost, index,margin, sale_price;
cost=Math.round(document.getElementsByName('kitcost')[0].value);
document.getElementsByName('unit_price')[0].value=cost/100;
index=document.forms[0].margin.selectedIndex;

margin=document.forms[0].margin[index].value;
document.getElementsByName('sale_price')[0].value=Math.round(
(Number(margin)+1000)*cost/1000)/100;
document.getElementsByName('bargain')[0].value=Math.round(
(Number(margin)+1000)*cost *0.9/1000)/100;
}

Note that multiplication and division of what I assume is an implicit
string, picked up from a form variable, worked as intended. Use of the
addition operator did not.

A simple choice of a dot operator rather than a plus for string
concatenation, as PHP does, would have made the whole language more
sensible.

Its sloppy ambiguity built into the language IMHO. If you throw away
typing, you ought to have a responsibility for making a given construct
unambiguous.

Use of overloaded operators in strongly typed language is fair enough:
the operator does what its intended according to the type of the operands.

In a weakly typed language its pretty much an unmitigated disaster as
far as I am concerned.

Still life is life, and one learns to work around other peoples shit
with the appropriate shovel.

Now come and tell me that Number(10) will return an error, because 10 is
not a string ...;-)
 
P

Peter Michaux

God., t took me an hour last night to add two numbers together..

Yo would have thought that the people who dreamed up this shit could
have made a slightly more sensible way of arranging typing than to
declare all variables typeless, and have a function to turn a typeless
thing into a number so that you could actually make '10'+'5' not be '105'...

<URL: http://www.jibbering.com/faq/#FAQ4_21>

It works as advertised: see Standard ECMA-262 section 11.6.1.

Peter
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
God., t took me an hour last night to add two numbers together..

Before trying to program in a language, one should try to learn
something about it and to appreciate how much or how little one knows.
Yo would have thought that the people who dreamed up this shit could
have made a slightly more sensible way of arranging typing than to
declare all variables typeless, and have a function to turn a typeless
thing into a number so that you could actually make '10'+'5' not be
'105'...

They did. If you had read the FAQ carefully you would have seen it. If
the FAQ were better written, you would not have needed to read it so
carefully. See also <URL:http://www.merlyn.demon.co.uk/js-
maths.htm#UPM>.

And there are occasions when combining '10' and '5' should give '105'...
 

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
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top