473,378 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

structure and effeciency on simple function advice please

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
7 992
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Donald Tyler | last post by:
I have a question that I hope I can explain well enough: I am trying to figure out a data structure for an inventory system. The system contains: Items Kits (Made from a collection of Items...
0
by: Mark Stokes | last post by:
Hi there, I wanted a bit of advice on a program (a prototype) that I am trying to write that uses threads. I will outline what I am attempting to do, and if anyone has any advice on the way I...
41
by: David Rasmussen | last post by:
I want to have an unsigned integer-like thing, that for now only allows the bitwise operators. I have defined these operators and their self-assigning cousins. This works, but I would like to...
3
by: ChadDiesel | last post by:
Hello everyone. I need some advice on table structure for a new project I've been given. One of our customers sends us an Excel spreadsheet each week containing their order. Currently, someone...
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
9
by: Brusex via DotNetMonster.com | last post by:
Hi I am trying to make to work this unmanaged Function from C to VB.Net My GetCarList Function returns always 0 (ERROR_NONE=0) but the structures are all empty. Can someone help me to resolve...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
2
by: David | last post by:
Hi all, using 1.1 I am creating a dynamic menu structure for my site, however, I may refer to the structure many times within a page. This would be fine if I was doing it all in the...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.