all i get is a NaN...

B

badstyle

newbie, learning some javascript...

started with this external script:
---------------------------------------------

var newNumber = document.form1.entry

function shout()
{
alert(newNumber + 5)
}

---------------------------------------------


within this html:
---------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="calculator-0001.js"></script>

</head>

<body>
<form name="form1" method="post" action="">
<input type="text" name="entry">
<input type="submit" name="addFive" value="+5" onClick="shout()">
</form>
</body>
</html>

---------------------------------------------

was hoping to get an alert with whatever number i put in the text with 5
added to it.

all i get is an alert with NaN.


Hopefully you can see where i'm going wrong??



...b..
 
B

badstyle

Lee said:
badstyle said:

var newNumber = document.form1.entry.value

cool thanks! got it sorted ;)


function shout()
{
var newNumber
newNumber = (document.form1.entry.value*1)+(5)
alert("the new number is: "+ newNumber)
}

just working on trying to create a working calculator now, then on to more
complex tasks...

chars

...b..
 
M

Mick White

badstyle said:
cool thanks! got it sorted ;)


function shout()
{
var newNumber
newNumber = (document.form1.entry.value*1)+(5)
alert("the new number is: "+ newNumber)
}
function shout(){
alert("the new number is: "+(+document.form1.entry.value+5))
}
You have used "newNumber" three times, when it is really not necessary
to do so.
And it is usually worthwhile to generalize your functions, if possible:

function addAndShout(val,addThis){
alert("the new number is: "+(+val+addThis))
}

Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 2
Sep 2004 21:55:37, seen in badstyle
function shout()
{
var newNumber
newNumber = (document.form1.entry.value*1)+(5)
alert("the new number is: "+ newNumber)
}


function shout() { var newNumber
newNumber = +document.form1.entry.value + 5
alert("The new number is: " + newNumber)
}


Code should be indented to show the intended structure, especially if
you expect anyone else to read it.

Your assignment line is unnecessarily complex.

See posted newsgroup FAQ, & via below.

BTW, using a blatantly false name is a technical newsgroup only makes
you look silly; and you should never give an E-mail address that you do
not have explicit permission to use.
 
B

badstyle

Dr John Stockton said:
JRS: In article <[email protected]>, dated Thu, 2
Sep 2004 21:55:37, seen in badstyle



function shout() { var newNumber
newNumber = +document.form1.entry.value + 5
alert("The new number is: " + newNumber)
}


Code should be indented to show the intended structure, especially if
you expect anyone else to read it.

Your assignment line is unnecessarily complex.

this is the very first sccript that i've attempted in javascript. It is
important for me to understand the processing of the information in my own
way first hence why it lacks efficiency too. "Correct" and a more refined
structure will come later the better and more proficient i become at
understanding the language and it's syntax. I can only do this with help
from people like you who are willing to point this out, so thank you but...
See posted newsgroup FAQ, & via below.

what am i looking for specifically within this??
BTW, using a blatantly false name is a technical newsgroup only makes
you look silly; and you should never give an E-mail address that you do
not have explicit permission to use.

in response to your statements:

1. i reserve my right to have real world personally identifiable information
kept to my self. even if i did change the "name" to 'bob cratchet' or
'stanley johnson' if my name was actually 'bob crachet' or 'stanley johnson'
it would still make no difference to the question being asked. lets try and
not get petty about very insignificant matters as "real" or false
identities.

In truth real identities are generally not important on the internet unless
you are making financial transations. As shakespeare once meant to state:
"What's in a name? That which we call a badstyle by any other name would
still be crap at javascript"

instead he famously stated: "What's in a name? That which we call a rose by
any other word would smell as sweet.".

2. point taken. the email is set to that so as not to recieve spam via mail
bots - in this day in age, a responsible move. unfortunately i stupidly used
an address i was not responsible for. The last email account i set up to use
on newsgroups became unusable due to this fact, so i've set up a real one to
catter for this inconvenience.




...b..
 
B

badstyle

Mick White said:
function shout(){
alert("the new number is: "+(+document.form1.entry.value+5))
}
You have used "newNumber" three times, when it is really not necessary to
do so.
And it is usually worthwhile to generalize your functions, if possible:

function addAndShout(val,addThis){
alert("the new number is: "+(+val+addThis))
}

Mick

interesting, how come the string "document.form1.entry.value" didn't need to
be turned into an interger by multiplying the value by 1?

I had originally tried a version like that which didn't work, is it the '+'
prior to the string which allows the value to be accepted as the interger?


...b..
 
R

Richard Cornford

badstyle said:
interesting, how come the string "document.form1.entry.value"
didn't need to be turned into an interger by multiplying the
value by 1?

The multiplication of a number-like string value by one is just one of
many ways of forcing type-conversion to a number. The subtraction of
zero, for example, achieves exactly the same result. In this case Mick
was using the unary plus operator to force the type-conversion.
I had originally tried a version like that which didn't work,
is it the '+' prior to the string which allows the value to
be accepted as the interger?

RTFM?

The expression - +"12" - is evaluated as the number 12 as the unary plus
operator type-converts its operand into a number (type). Unary plus is
the fastest method of type-converting a string into a number as it does
not involve any additional mathematical operations. Its major drawback
is that it is not particularly clear when it appears in source code
(especially when mixed in statements containing addition or
concatenation operations). So where Mick wrote:-

(+document.form1.entry.value+5)

- I would have written:-

((+document.form1.entry.value) + 5)

- parenthesising the unary plus expression so that it was clear that was
unrelated to the following addition (or the preceding concatenation).

Where source code clarity is more important than speed of execution
type-converting to a number is best done by using the Number constructor
as a function:-

( Number(document.form1.entry.value) + 5)

It is about 4 times slower than the (optimum) unary plus operator at
performing the type-conversion but virtually self-documenting.

With unary plus offering the ultimate in speed and the Number
constructor (called as a function) offering the ultimate in clarity,
there is no good reason to be using any other type-converting-to-number
methods when writing javascript. (non-type-converting parseInt and
parseFloat have their own place and applications.)

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 4
Sep 2004 10:57:07, seen in badstyle <phatwap-
(e-mail address removed)> posted :
what am i looking for specifically within this??

Everything. It will save your time and it will save our time. Read not
only what is cited, but all links pertinent to your interests and
intended actions.

in response to your statements:

1. i reserve my right to have real world personally identifiable information
kept to my self. even if i did change the "name" to 'bob cratchet' or
'stanley johnson' if my name was actually 'bob crachet' or 'stanley johnson'
it would still make no difference to the question being asked. lets try and
not get petty about very insignificant matters as "real" or false
identities.


Thos who give real identities are more respected than those who do not,
on the average and with few exceptions. There is nothing to stop you
from using a consistent, plausible identity. Your present one merely
proclaims your immaturity, as does your slovenly typing style.


Don't quote signatures, unless discussing them,
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top