Javascript String validNum()

Description

Javascript String validNum()


var testNum = "1234 0213 1234 6815";

function getSumdigits(num) {
 var arr = num.toString().split("");
 return parseInt(arr[0]) + parseInt(arr[1]);
}

String.prototype.validNum = function() {
 debugger;/* w ww  .  ja v  a2  s .  c o m*/
 var str = this.split(" ").join("").split(""),
 sum = 0;
 arr = [];
 
 for (var i = 0, l = str.length; i < l; i++) {
  arr.push(parseInt(str[i]));
 }
 
 arr = arr.reverse();

 for (var i = 0; i < l; i++) {
  
  if (i % 2 == 1) {
   arr[i] *= 2;
   if (arr[i] > 9) {
    sum += arr[i].getSumdigits();
   } else {
    sum += arr[i];
   }
  } else {
   sum += arr[i];
  }
 }
 return sum;
}

Javascript String validNum()

function getSumDigits(num) {
 var arr = num.toString().split("");
 return parseInt(arr[0]) + parseInt(arr[1]);
}

String.prototype.validNum = function() {

 var str = this.split(" ").join("").split(""),
 sum = 0,//from w  w w.j a v a  2 s .c  o m
 arr = [];
 
 for (var i = 0, l = str.length; i < l; i++) {
  arr.push(parseInt(str[i]));
 }
 
 arr = arr.reverse();

 for (var i = 0; i < l; i++) {
  
  if (i % 2 == 1) {
   arr[i] *= 2;
   if (arr[i] > 9) {
    sum += getSumDigits(arr[i]);
   } else {
    sum += arr[i];
   }
  } else {
   sum += arr[i];
  }

 }

 if (sum % 10 == 0) {
  return 1;
 } else {
  return 0;
 }

}

console.log(line.validNum());



PreviousNext

Related