Re: ISBN validation for idiots...
Re: ISBN validation for idiots...
- Subject: Re: ISBN validation for idiots...
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 14 May 2007 09:24:09 -0400
On 5/14/07, Ruth Bygrave <email@hidden> wrote:
So I looked at the javascript, which said:
-------------
if (widget)
widget.openURL(url + s)
-------------
Even I can nearly figure it out, I thought, and I changed it to:
------------
if (widget)
s=s.isbn10(s)
widget.openURL(url + s)
------------
Two things:
1) you're confusing functions and methods. You're defining a
standalone isbn10 function, which is called as just isbn10(s), no
"s." in front. The latter is for object-oriented stuff where you
define isbn10 as a method inside one of s's prototype chain, which you
probably don't want to mess with. (As an aside, though, if you did,
s.isbn10(s) would be redundant; it'd just be s.isbn10(), since a
method can always tell what object it was called on.)
2) You can only put one statement after the "if" in JavaScript and
other C-derived languages. There's no "end if". So if you want to do
more than one thing, you have to create a compound statement using
curlies:
if (widget)
{
s = isbn10(s);
widget.openURL(url + s);
}
or you could just do this:
if (widget)
widget.openURL(url + isbn10(s))
In your version, the widget.openURL gets called even if widget is
undefined, which is probably the source of your errors.
if (s.substring(0,3) == "978")
{
s = s.substring(0,11);
Hm. No matter how you think "substring" works (and there are a wide
variety of ways that substring functions work in different languages),
that combination doesn't make sense. If substring(0,X) returns the
first X characters (as implied by your if conditional) then the (0,11)
substring keeps the 978 but lops off the last two characters of the
string... I assume you want to get rid of both the 978 and the check
digit, but keep the rest. That'd be s.substring(3,12).
Try this:
function isbn10(s)
{
if (s.substring(0,3) == "978")
{
s = s.substring(3, 12);
var checkSum = 0;
for (var i=0; i<9; ++i)
{
checkSum += s.substring(i,i+1) * (i+1)
}
checkSum %= 11;
var checkDigit = checkSum == 10 ? "X" : checkSum;
return s + checkDigit;
}
}
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden