I'm trying to put a company name in front of a card number, this code here is what i'm attempting to make it print the name in front. The problem i'm having here is that each company has rules - conditions, i'm struggling to make these rules into conditions for an if statement.
I realise the if statement i'm doing is not a good way to handle this, but i'm just asking for an explanation on the best way to approach this.
AMEX card rules: - Length is 15 - Starts with '34' or '37'
Discover card rules: - Length is 16 - Starts with '6011'
MasterCard card rules: - Length is 16 - Starts with '51' or '55'
VISA card rules: - Starts with '13' or '16' or '4'
I'm reading numbers from another file seen in the code, i didn't think i'd have to include this but this is why i'm having trouble I'm not trying to skip an if statement just saying mine is impractical
This is my code:
import { createReadStream } from 'fs'
import { createInterface } from 'readline'
const lineReader = createInterface({
input: createReadStream('data/input.txt')
})
lineReader.on('line', (creditCard: string) => {
if ((creditCard.length === 15 && creditCard.substring(0, 1) === '37')) {
creditCard = '(AMEX) ' + creditCard + ' valid'
} else {
return
}
console.log('Line from file: ', creditCard)
})