By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

structure and effeciency on simple function advice please

P: n/a
Can this be refined further to be more efficient?

function shout()
{
if (isNaN(document.form1.entry.value)==true) {
alert("please only enter numbers for an answer")}
else
{
alert("the new number is: "+(+document.form1.entry.value+5))}
}
also what would be the correct structure as to make the script more legible
to regular and proficient writers of JS?

thanks!

...b..
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Sat, 11 Sep 2004 14:54:12 +0000 (UTC), badstyle
<ph************@yahoo.co.uk> wrote:
Can this be refined further to be more efficient?
If there was more to this function, there might be other optimisations.
Was there anything in particular you were considering?
function shout()
{
if (isNaN(document.form1.entry.value)==true) {
It would be simpler to use:

if(isNaN(...)) {

The comparison is unnecessary. However, it's not the best way to check for
numeric input. I'll present a better approach below, but it's very generic
and might not be what you'd really want.
alert("please only enter numbers for an answer")}
else
{
alert("the new number is: "+(+document.form1.entry.value+5))}
As you use the element reference more than once, it's best to save that
reference. It avoids the need to resolve properties repeatedly.
also what would be the correct structure as to make the script more
legible to regular and proficient writers of JS?


Try:

function shout() {
// Save reference to form element
var e = document.forms['form1'].elements['entry'];
// Check for only integers
if(!/^\d+$/.test(e.value)) {
alert('Please restrict values to numbers only.');
} else {
alert('The new number is ' + (+e.value + 5) + '.');
}
}

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
Michael Winter wrote on 11 sep 2004 in comp.lang.javascript:
function shout() {
// Save reference to form element
var e = document.forms['form1'].elements['entry'];
// Check for only integers
if(!/^\d+$/.test(e.value)) {
alert('Please restrict values to numbers only.');
} else {
alert('The new number is ' + (+e.value + 5) + '.');
}
}


Slightly more optimalisation is possible:

function shout() {
var e = document.forms['form1'].elements['entry'].value;
alert(
(/^\d+$/.test(e))?
'The new number is ' + (+e + 5) + '.':
'Please restrict values to numbers only.')
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #3

P: n/a
On 11 Sep 2004 16:10:23 GMT, Evertjan. <ex**************@interxnl.net>
wrote:

[snip]
function shout() {
var e = document.forms['form1'].elements['entry'].value;
I made the assumption that if there was a new number, the element might
want to be modified, or accessed in some other way, like calling focus
should the validation fail. But yes, if only the value is needed, then
save that.
alert(
(/^\d+$/.test(e))?
'The new number is ' + (+e + 5) + '.':
'Please restrict values to numbers only.')


I can't say I'd have considered that, but then again, it's a rather
artificial example and I don't think it would occur in a real setting.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

P: n/a
Michael Winter wrote on 11 sep 2004 in comp.lang.javascript:
alert(
(/^\d+$/.test(e))?
'The new number is ' + (+e + 5) + '.':
'Please restrict values to numbers only.')


I can't say I'd have considered that, but then again, it's a rather
artificial example and I don't think it would occur in a real setting.


'cause not, but the OP specified "more efficient".

My solution will be unreadable for the newbee, I presume.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #5

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
alert(
(/^\d+$/.test(e))?
'The new number is ' + (+e + 5) + '.':
'Please restrict values to numbers only.')


I can't say I'd have considered that, but then again, it's a rather
artificial example and I don't think it would occur in a real setting.


'cause not, but the OP specified "more efficient".

My solution will be unreadable for the newbee, I presume.


you're right, to me it is extremely unreadable at the moment! :)

I understand what it is [in my mind] supposed to be doing, based on my
meagre attempt and knowing that allows me to dissect this more "tech savvy"
interpretation and get my mind into understanding these alternative methods.

Thanks people, much appreciated! :)
...b..
Jul 23 '05 #6

P: n/a
JRS: In article <ch**********@hercules.btinternet.com>, dated Sat, 11
Sep 2004 14:54:12, seen in news:comp.lang.javascript, badstyle <phatwap-
sp****@yahoo.co.uk> posted :
Can this be refined further to be more efficient?
Yes.
function shout()
{
if (isNaN(document.form1.entry.value)==true) {
alert("please only enter numbers for an answer")}
else
{
alert("the new number is: "+(+document.form1.entry.value+5))}
}
also what would be the correct structure as to make the script more legible
to regular and proficient writers of JS?


"Never" use ==true (except as an indicator of ignorance).

"Numbers" includes such as -1.2345e6, 5e9999, funf-und-funfzig, MCMXLIV,
0x3F5. Perhaps you mean decimal digits? isNaN is generally an
inadequate test. See my js-valid.htm.

Indent two or three spaces, consistently, for each currently-unfinished
language construct.

Use var Ent = document.form1.entry

Omit +( and one ) . Put spaces around concatenating + .

Reword the failure alert.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

P: n/a
badstyle wrote:
^^^^^^^^
Your "name" is program?
Can this be refined further to be more efficient?

function shout()
{
if (isNaN(document.form1.entry.value)==true) {
alert("please only enter numbers for an answer")}
else
{
alert("the new number is: "+(+document.form1.entry.value+5))}
}

also what would be the correct structure as to make the script more
legible to regular and proficient writers of JS?


<head>
...
<script type="text/javascript">
function shout(f, sName)
{
var o = null;
if (f && (o = f.elements) && (o = o[sName]))
{
if (isNaN(o.value))
{
alert("Please only enter numbers for an answer.");
}
else
{
alert("The new number is: " + (+o.value + 5));
}
}
}
</script>
...
</head>

<body>
...
<form action="...">
...
<input name="entry">
...
<script type="text/javascript">
document.write(
'<input type="button" onclick="shout(this.form, \'entry\')">');
</script>
...
</form>
...
</body>
PointedEars
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.