Sha256: 7ccf623a4de4d8f518132e418db4ea20e624f4afb0c6c80630029e1e06f9c3c9

Contents?: true

Size: 786 Bytes

Versions: 63

Compression:

Stored size: 786 Bytes

Contents

class Luhn {
    static valid(value: string): boolean {
        const numbers = value.replace(/\s/g, '')
        const digits = [...numbers]

        const sum = digits
            // convert to integers
            .map((d) => parseInt(d, 10))
            // double even positions (odd indexes)
            .map((d, i) => {
                if (i % 2 !== 0) {
                    return d * 2
                }
                return d
            })
            // limit to digits less than 10
            .map((d) => {
                if (d > 9) {
                    return d - 9
                }
                return d
            })
            // sum all digits
            .reduce((d, acc) => d + acc, 0)

        return sum > 0 && sum % 10 === 0
    }
}

export default Luhn

Version data entries

63 entries across 63 versions & 1 rubygems

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