Dailyprogrammer challenge #197 - Validating ISBN Numbers
Hello again!
I now have a reddit account. You can find it here: https://www.reddit.com/user/starbeamrainbowlabs
I have attempted the latest Daily Programmer challenge.
This time I have written it in javascript. The challenge was to validate an ISBN-10 number. To validate an ISBN-10 number, you add 10 times the first number to 9 times the second number to 8 times the third number and so on. This total should leave no remainder when divided by 11. In addition, the letter X
stands for a value of 10.
Here is my solution:
function validate_isbn(isbn) {
var i = 10,
tot = isbn.replace(/-/g, "").split("").reduce(function (total, char) {
if (char.toLowerCase() == "x")
total += i * 10;
else
total += i * parseInt(char);
i--;
return total;
}, 0);
if (tot % 11 === 0)
return true;
else
return false;
}
I minified it by hand too:
function validate_isbn(a){var i = 10;if(a.replace(/-/g,"").split("").reduce(function(b, c){if(c.toLowerCase()=="x")b+=i*10;else b+=i* parseInt(c);i--;return b;},0)%11==0)return true;else return false;}
I should probably attempt the next challenge in C♯ so that I keep practising it.
The daily programmer challenge can be found here: Daily Programmer Challenge #197 - ISBN Validator