basic-js icon indicating copy to clipboard operation
basic-js copied to clipboard

In some cases tests just don't run

Open Freemason-12 opened this issue 1 year ago • 0 comments

I have been having this issue with some other basicJS problems as well, curently having it with vigenere cipher When I run npm run test i get:

> [email protected] test
> mocha

and test just ends with no errors.

Here is my code for this problem:

const { NotImplementedError } = require('../extensions/index.js');

/**
 * Implement class VigenereCipheringMachine that allows us to create
 * direct and reverse ciphering machines according to task description
 * 
 * @example
 * 
 * const directMachine = new VigenereCipheringMachine();
 * 
 * const reverseMachine = new VigenereCipheringMachine(false);
 * 
 * directMachine.encrypt('attack at dawn!', 'alphonse') => 'AEIHQX SX DLLU!'
 * 
 * directMachine.decrypt('AEIHQX SX DLLU!', 'alphonse') => 'ATTACK AT DAWN!'
 * 
 * reverseMachine.encrypt('attack at dawn!', 'alphonse') => '!ULLD XS XQHIEA'
 * 
 * reverseMachine.decrypt('AEIHQX SX DLLU!', 'alphonse') => '!NWAD TA KCATTA'
 * 
 */
    // throw new NotImplementedError('Not implemented');
// write this to each method to mute it ^^^


class VigenereCipheringMachine {
  encrypt(message = null, key = null) {
    if(message === null || key === null) throw new Error('Incorrect arguments!')
    message = message.toUpperCase(); key = key.toUpperCase()
    
    let enc = [], ml = message.length, kl = key.length
    for(let i = 0; i < ml; i++){
      let x = message[i].charCodeAt(0) - 65, y = key[i % kl].charCodeAt(0) - 65
      enc.push((x >= 0 && x < 26)? this.table[x][y] : message[i])
    }
    return (this.direct)? enc.join("") : enc.reverse().join("")
  }
  decrypt(encrypted = null, key = null) {
    if(encrypted === null || key === null) throw new Error('Incorrect arguments!')
    encrypted = encrypted.toUpperCase(); key = key.toUpperCase()

    let dec = [], el = encrypted.length, kl = key.length
    for(let i = 0; i < el; i++){
      let x = message[i].charCodeAt(0) - 65
      dec.push(this.table[0][this.table[x].indexOf(key[i])])
    }
    return (this.direct)? dec.join("") : dec.reverse().join("")
  }
  constructor(dir = true){
    this.direct = dir
    this.table = generateTable()
  }
}

function generateTable(){
    let a = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    let res = []
    res.push(a)
    for(let i = 0; i < 25; i++){
      let cp = [...res[res.length - 1]]
      cp.push(sp.shift())
      res.push(cp)
    }
    return res
  }

module.exports = {
  VigenereCipheringMachine
};

it runs normally and shows errors (as it should) when I remove the constructor from the class

Freemason-12 avatar Apr 15 '23 10:04 Freemason-12