-1

I have a code below that matches a remainder of an 8 digit number to a letter when divided by 23.

function dniLetter( dni ) {

var lockup = 'TRWAGMYFPDXBNJZSQVHLCKE'
var result = ''; 
var remainder = dni % 23;
result = lockup.charAt(remainder)

return result; }

How could I improve if the number starts with a negative number (like -2) or start with a letter (A1234567)?

jso1226
  • 59
  • 1
  • 2
  • 8

2 Answers2

1

For the negative numbers, you should try replacing dni % 23 by ((dni % 23) + 23) % 23. It will do exactly what you want.

Benjamin Amelot
  • 103
  • 1
  • 8
0

You should use regular expressions.

/^[a-zA-Z-]/.test(yourString) returns true on the specified conditions (and even if it starts with '-A' for instance).