Sha256: e72e3f508510a83f91f4167423aaa3ba319c97f8cfed0198b9aeebf9454cdf8d

Contents?: true

Size: 497 Bytes

Versions: 59

Compression:

Stored size: 497 Bytes

Contents

class ISBN {
    isbn: string

    constructor(isbn: string) {
        this.isbn = isbn.replace(/-/g, '')
    }

    isValid() {
        if (!this.isbn.match(/^(\d{9}[\dxX])$/)) {
            return false
        }

        const digits = [...this.isbn]
        if (digits[9].match(/[xX]/)) {
            digits[9] = '10'
        }

        const sum = digits.reduce((acc, value, index) => acc + ((10 - index) * parseInt(value, 10)), 0)

        return sum % 11 === 0
    }
}

export default ISBN

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/isbn-verifier/isbn-verifier.example.ts