DEV Community

Discussion on: Daily Challenge #292 - ISBN-10 Identifiers

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with JavaScript and using for loop to resolve this challenge :).

function validISBN10(isbn) {
  let index = 0;
  let checkSum = 0;
  let xCount = 0;
  for (; index < isbn.length; index++) {
    if (isbn[index] === 'X') {
      xCount += 1;
    }
    checkSum += parseInt(isbn[index]) * (index+1);
  }

  if (isbn.length !== 10) {
    return false;
  }

  if (isbn[isbn.length-1] === 'X' && xCount === 1) {
    return true;
  }

  return checkSum % 11 === 0;
}