what might me be wrong?
Moderator: General Moderators
what might me be wrong?
I have a javascript program (http://pastebin.com/YtyNsgWC if you really want to see the detail)
went wrong. The only message I got fromm firfox console is
[09:32:19.148] str.match is not a function
But that is a system built-in function.
So, what might me be wrong?
went wrong. The only message I got fromm firfox console is
[09:32:19.148] str.match is not a function
But that is a system built-in function.
So, what might me be wrong?
Re: what might me be wrong?
isbn stopped being a string when you ++ed it. Along those lines, if the ISBN number had an X in it then you'd get unexpected results.
Re: what might me be wrong?
Yes, it is type conversion problem.
I modified it http://pastebin.com/7VXTS0G2
although the program look awkward.
1. Is there anyway to do the job more elegantly?
2. There is still error message saying $ is undefined. $ is jQuery library, which is already included.
I modified it http://pastebin.com/7VXTS0G2
although the program look awkward.
1. Is there anyway to do the job more elegantly?
2. There is still error message saying $ is undefined. $ is jQuery library, which is already included.
Re: what might me be wrong?
Possibly but I don't see enough "wrong" with that code to warrant changing it. It has a simple structure, doesn't use too many variables or too much code, and (I assume) gets the right result.
Does "jQuery" exist? If not then the library simply isn't being included. Are you sure it's currentfolder/js/jquery.js and not, say, /js/jquery.js?
Does "jQuery" exist? If not then the library simply isn't being included. Are you sure it's currentfolder/js/jquery.js and not, say, /js/jquery.js?
Re: what might me be wrong?
Yes, I indeed found what is wrong. jQuery is not there! Those error messages are misleading. I came back to modify my
question but you have replied.
So, my question remains: is there any way to write the code more elegantly ?
Is there a way to handle, like binary, octa, etc for the decimal+1 (which I do not know how to write in English, the number system with X for 10)?
By the way, is there anyway to stop an infinite loop javascript more elegantly?
As soon as I correct the jQuery location, I came into a long loop. I tried to kill the browser, stop the internet but no use.
Eventually, I turned the machine off.
question but you have replied.
So, my question remains: is there any way to write the code more elegantly ?
Is there a way to handle, like binary, octa, etc for the decimal+1 (which I do not know how to write in English, the number system with X for 10)?
By the way, is there anyway to stop an infinite loop javascript more elegantly?
As soon as I correct the jQuery location, I came into a long loop. I tried to kill the browser, stop the internet but no use.
Eventually, I turned the machine off.
Re: what might me be wrong?
Of the three loops in there, if one of them was infinite then I would first look at the one in start().
Otherwise, you're really looking to rewrite it? I see
1. The while loop is unnecessary - you can replace all hyphens in one command
2. It accepts Xs in places where they don't belong (ie, everywhere but at the end of an ISBN-10 number)
3. tmp is unneeded
4. I really don't like the loop in start(). Why is it there?
If I were to write chk_ISBN() then I would write it like
Otherwise, you're really looking to rewrite it? I see
1. The while loop is unnecessary - you can replace all hyphens in one command
2. It accepts Xs in places where they don't belong (ie, everywhere but at the end of an ISBN-10 number)
3. tmp is unneeded
4. I really don't like the loop in start(). Why is it there?
If I were to write chk_ISBN() then I would write it like
Code: Select all
function chk_ISBN(str) {
// remove hyphens
str = str.replace(/-/g, "");
var sum = 0;
if (str.test(/^[0-9]{9}[0-9X]$/i)) {
// ISBN-10
for (var i = 0; i < 9; i++) { // last digit handled separately
sum += str[i] * (10 - i);
}
sum += (str[9] == 'X' || str[9] == 'x' ? 10 : str[9]);
return (sum % 11) == 0;
} else if (str.test(/^[0-9]{13}$/)) {
// ISBN-13
for (var i = 0; i < 13; i++) {
sum += str[i] * ((i % 2) == 0 ? 1 : 3);
}
return (sum % 10) == 0;
} else {
return false;
}
}Re: what might me be wrong?
The chk_ISBN is copied from elsewhere, not by me. I will check later, since I am not very familiar with regular expression.
Will ISBN 13 has no x or X?
The ugly start() is by me
The main question is how to make the ugly start nice
The program is trying to iterate through a series of ISBNs, check if they are valid, if valid submit to the server to get
the book information and save in the database.
The problem is at the iteration. I cannot iterate using PHP or HTML, that will generate many useless pages and
page reload, to my understanding. In the HTML, I only need to enter a starting ISBN. Once the number is entered, I call a javascript program start() to do the iteration. If the ISBN is valid call send_findbooks, which will generate a call $.getJSON
To my understanding, such a call could also be generated by <form action="..."> but the problem is that will need many
form generation. Therefore, I am left with the only option, do by javascript.
Will ISBN 13 has no x or X?
The ugly start() is by me
The main question is how to make the ugly start nice
The program is trying to iterate through a series of ISBNs, check if they are valid, if valid submit to the server to get
the book information and save in the database.
The problem is at the iteration. I cannot iterate using PHP or HTML, that will generate many useless pages and
page reload, to my understanding. In the HTML, I only need to enter a starting ISBN. Once the number is entered, I call a javascript program start() to do the iteration. If the ISBN is valid call send_findbooks, which will generate a call $.getJSON
To my understanding, such a call could also be generated by <form action="..."> but the problem is that will need many
form generation. Therefore, I am left with the only option, do by javascript.
Re: what might me be wrong?
Check the Wikipedia page.wvoyance wrote:Will ISBN 13 has no x or X?
But why? Why does it have to loop over potential ISBN numbers? Is the input not supposed to be the ISBN number, when you then validate and pass off to the server?wvoyance wrote:The program is trying to iterate through a series of ISBNs, check if they are valid, if valid submit to the server to get
the book information and save in the database.
Yeah you can. Like how you're looping in JavaScript you can loop in PHP. You don't have to load a new PHP page for every single one.wvoyance wrote:I cannot iterate using PHP or HTML
Re: what might me be wrong?
But why? Why does it have to loop over potential ISBN numbers? Is the input not supposed to be the ISBN number, when you then validate and pass off to the server?wvoyance wrote:The program is trying to iterate through a series of ISBNs, check if they are valid, if valid submit to the server to get
the book information and save in the database.
I am trying to build a book store. Even for those books I do not have in stock, I would like to have information
stored in my database. Therefore I tried to iterate through all numbers.
Yeah you can. Like how you're looping in JavaScript you can loop in PHP. You don't have to load a new PHP page for every single one.[/quote]wvoyance wrote:I cannot iterate using PHP or HTML
Please tell me how. My knowledge and experience in PHP etc is limited. Thanks.
Re: what might me be wrong?
I'm not a fan of the "I don't have these books but I'm going to record them in my database anyways" idea, but whatever.
PHP has for loops too.
Take the time to learn about the language before you dive head-first into something you don't know how to do.
PHP has for loops too.
Code: Select all
for ($isbn = /* some starting point */; $isbn < /* some ending point */; $isbn++) {Re: what might me be wrong?
Yes, I know such loop in PHP. Yes, it will not reload the page.
But that means it will generate a long HTML code.
Suppose I iterate through 100000 isbns...about 10000 will be success.
That will be a long HTML code even if each one is only a few lines.
Furthermore, the submit of such HTML to server will cause the server to run a long time.
To my understanding, ever server has a limit for the execution time to about 10s, which will not be enough to access 10000 book information from libraries.
But that means it will generate a long HTML code.
Suppose I iterate through 100000 isbns...about 10000 will be success.
That will be a long HTML code even if each one is only a few lines.
Furthermore, the submit of such HTML to server will cause the server to run a long time.
To my understanding, ever server has a limit for the execution time to about 10s, which will not be enough to access 10000 book information from libraries.
Re: what might me be wrong?
You wouldn't have to scan all 100k of them: the last digit is a checksum digit, so loop through the first 9 (or 12) digits and calculate the last one.
Re: what might me be wrong?
yes, that is true....perhaps i need to modify this part of my program. Pure iteration is simple.
But that is not the point.
Even if you submit 2 or 3 ISBNs together in one form the processing time exceed the server's 10s limit.
I actually tested. To process one book, i.e. tried to copy the image from some 5 possible site
and get other information from a library, need about 3 to 5 seconds.
I am not sure about other site. But my free-hosting website has this limit for 10s.
But that is not the point.
Even if you submit 2 or 3 ISBNs together in one form the processing time exceed the server's 10s limit.
I actually tested. To process one book, i.e. tried to copy the image from some 5 possible site
and get other information from a library, need about 3 to 5 seconds.
I am not sure about other site. But my free-hosting website has this limit for 10s.
Re: what might me be wrong?
If I were to write chk_ISBN() then I would write it like
[/quote]
I am not sure whether is this program only for illustrative purpose?
Or, read to run?
I tried to run it and encountered several problem.
Should there be a function called str.test? I have to use:
var pattern = /^[0-9]{12}$/;
if (pattern.test(str)){...
Furthermore, there seems to have type conversion problems.
If you use str, what you get should be a string.
Will it automatically convert into integer?
Since there are at most 10 or 13 digits
I think it will be quicker if
sum += str * ((i % 2) == 0 ? 1 : 3);
is replaced with
sum = (str[0].parseInt+str[2].parseInt+str[4].parseInt+str[6].parseInt+str[8].parseInt+str[10].parseInt)+3*(str[1].parseInt+str[3].parseInt+str[5].parseInt+str[7].parseInt+str[9].parseInt+str[11].parseInt);
Code: Select all
function chk_ISBN(str) {
// remove hyphens
str = str.replace(/-/g, "");
var sum = 0;
if (str.test(/^[0-9]{9}[0-9X]$/i)) {
// ISBN-10
for (var i = 0; i < 9; i++) { // last digit handled separately
sum += str[i] * (10 - i);
}
sum += (str[9] == 'X' || str[9] == 'x' ? 10 : str[9]);
return (sum % 11) == 0;
} else if (str.test(/^[0-9]{13}$/)) {
// ISBN-13
for (var i = 0; i < 13; i++) {
sum += str[i] * ((i % 2) == 0 ? 1 : 3);
}
return (sum % 10) == 0;
} else {
return false;
}
}I am not sure whether is this program only for illustrative purpose?
Or, read to run?
I tried to run it and encountered several problem.
Should there be a function called str.test? I have to use:
var pattern = /^[0-9]{12}$/;
if (pattern.test(str)){...
Furthermore, there seems to have type conversion problems.
If you use str, what you get should be a string.
Will it automatically convert into integer?
Since there are at most 10 or 13 digits
I think it will be quicker if
sum += str * ((i % 2) == 0 ? 1 : 3);
is replaced with
sum = (str[0].parseInt+str[2].parseInt+str[4].parseInt+str[6].parseInt+str[8].parseInt+str[10].parseInt)+3*(str[1].parseInt+str[3].parseInt+str[5].parseInt+str[7].parseInt+str[9].parseInt+str[11].parseInt);
Re: what might me be wrong?
Hopefully to understand it, but you could use it.wvoyance wrote:I am not sure whether is this program only for illustrative purpose?
Or, read to run?
There is a non-zero chance of bugs in the code. I wrote it quite quickly and probably should have tested it first.wvoyance wrote:Should there be a function called str.test? I have to use:
var pattern = /^[0-9]{12}$/;
if (pattern.test(str)){...
Furthermore, there seems to have type conversion problems.
wvoyance wrote:If you use str, what you get should be a string.
Will it automatically convert into integer?
It can, depending on what you do to it. But when something is ambiguous, values tend to be converted to strings rather than to numbers.
In this case it doesn't. 1 + "1" == "11"
wvoyance wrote:Since there are at most 10 or 13 digits
I think it will be quicker if
sum += str * ((i % 2) == 0 ? 1 : 3);
is replaced with
sum = (str[0].parseInt+str[2].parseInt+str[4].parseInt+str[6].parseInt+str[8].parseInt+str[10].parseInt)+3*(str[1].parseInt+str[3].parseInt+str[5].parseInt+str[7].parseInt+str[9].parseInt+str[11].parseInt);
Quite possibly, yes.