Sha256: 9b046f93b93c6db3d6db749496a949faa4e5682d2cd05561955112e5cf9674cd

Contents?: true

Size: 1.38 KB

Versions: 67

Compression:

Stored size: 1.38 KB

Contents

class SimpleCipher {
    key: string

    constructor(key?: string) {
        if (key === undefined) {
            for (let i = 0; i < 100; i++) {
                this.key += String.fromCharCode(Math.random() * 26 + 97)
            }
        } else {
            this.key = key!
        }
        if (!/[a-z]/.test(key!)) {
            throw new Error('Bad key')
        }
    }

    encode(decodedMessage: string): string {
        let encodedMessage = ''
        for (let i = 0; i < decodedMessage.length; i++) {
            let encodedChar = String.fromCharCode(decodedMessage.charCodeAt(i) + (this.key.charCodeAt(i % this.key.length) - 97))
            if (encodedChar.charCodeAt(0) > 122) {
                encodedChar = String.fromCharCode(encodedChar.charCodeAt(0) - 26)
            }
            encodedMessage += encodedChar
        }
        return encodedMessage
    }

    decode(encodedMessage: string): string {
        let decodedMessage = ''
        for (let i = 0; i < encodedMessage.length; i++) {
            let decodedChar = String.fromCharCode(encodedMessage.charCodeAt(i) - (this.key.charCodeAt(i % this.key.length) - 97))
            if (decodedChar.charCodeAt(0) < 97) {
                decodedChar = String.fromCharCode(decodedChar.charCodeAt(0) + 26)
            }
            decodedMessage += decodedChar
        }
        return decodedMessage
    }
}

export default SimpleCipher

Version data entries

67 entries across 67 versions & 1 rubygems

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