javascript拆分数字和字母的var

时间:2014-10-31 05:17:57

标签: javascript regex

我更喜欢不使用正则表达式,但如果需要,也可以使用正则表达式。

我有一些代码,我想取一个用户的输入并检查以确保它是一个isbn 10.换句话说,它必须是一个10位数字或一个9位数字,其中x为结尾(x代表数字10)。出于我的目的,我想将用户输入转换为每个数字的数组。如果有一个x我想将其改为10.我无法做到这一点!我见过其他有些相似的问题,他们都使用正则表达式。就像我说的,我更喜欢不使用正则表达式,但如果需要的话......

    <h1>Problem #3:</h1>

    <form name= "form">
        <input id= "input" name= "isbn" type="number" placeholder="Enter your ISBN-10" min="0" />
        <input id= "button" type="button" name="Validate" value="Validate" />
    </form>

    <div id="validISBN">
        Valid ISBN
    </div>
    <div id="invalidISBN">
        Invalid ISBN
    </div>
    <script src="js/jquery-2.0.3.min.js"></script>
    <script>
        $( document ).ready(function() {
            alert("Welcome to ISBN Validator!");
            //Add the event listener for the validate button here
            //Look at toggling the CSS display property based on the result
            $("#button").click(function(){
                checker(document.form.isbn.value);
            });
        });
var checker = function(isbn){
    isbn = isbn.toString().split('');
    if (isbn[9] == 'x'|| isbn[9] == 'X') {
        isbn[9] = 10;
    }
    if (isbn.length !== 10) {
        alert("invalid ISBN!" + isbn.length); 
    }
    else{
        var sum = 0;
        for (var x=10; x>0; x--){
            sum += x*isbn[10-x];
        }
        alert("FINAL!!" + sum%11);
    }
}

输入:0375726721 输出:FINAL !! 0 :工程

输入:067978330X 预期输出:FINAL !! 0 实际输出:无效的ISBN!0 :不起作用!

3 个答案:

答案 0 :(得分:0)

var isbn = '074759582x';

if (!/^\d{9}(\d|x)$/i.test(isbn)) // validation with regexp
    alert('Invalid ISBN');
else {
   var arr = isbn.split('');
   arr[9] = arr[9].toLowerCase() == 'x' ? 10 : arr[9]; // replacement of x by 10

   // below is your summation, just performed in another way. 
   var total = arr.map(function(el, index, arr) {
      return (index + 1) * arr[10 - index - 1];
   }).reduce(function(a, b) {return a + b;});

   alert(total % 11);
}

完成

答案 1 :(得分:0)

问题#3:

<form name= "form">
    <input id= "input" name= "isbn" type="number" placeholder="Enter your ISBN-10" min="0" />
    <input id= "button" type="button" name="Validate" value="Validate" onclick = "checker()" />
</form>

<div id="validISBN">
    Valid ISBN
</div>
<div id="invalidISBN">
    Invalid ISBN
</div>

<script>
     function checker () {            
         isbn = document.form.isbn.value;
        isbn = isbn.toString().split('');
        if (isbn[9] == 'x' || isbn[9] == 'X') {
            isbn[9] = 10;
        }
        if (isbn.length !== 10) {
            alert("invalid ISBN!" + isbn.length);
        }
        else {
            var sum = 0;
            for (var x = 10; x > 0; x--) {
                sum += x * isbn[10 - x];
            }
            alert("FINAL!!" + sum % 11);
        }
    }
     </script>

答案 2 :(得分:0)

var isbn = '074759582x';

使用split将字符串拆分为字符。应用地图抓取x并在必要时将其转换为10。然后将每个字符映射到数字

array = isbn
    .split('') 
    .map(function(char, i) { 
        return i === 9 && char.toLowerCase() === 'x' ? 10 : char;
    })
    .map(Number)
;

如果长度为10,则ISBN有效,并且其中没有NaN。

valid = array.length === 10 && !array.some(isNaN);

Checksum使用reduce,如另一个答案:

sum = array.reduce(function(result, v, i) {
    return result + (10-i) * v;
}, 0) % 11;
相关问题