{"size": 1689, "ext": "js", "max_stars_repo_path": "src/vigenere-cipher.js", "max_stars_repo_name": "AdamTasten/basic-js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/vigenere-cipher.js", "max_issues_repo_name": "AdamTasten/basic-js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/vigenere-cipher.js", "max_forks_repo_name": "AdamTasten/basic-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.6315789474, "max_line_length": 78, "alphanum_fraction": 0.5210183541}
class VigenereCipheringMachine {
  constructor(flag) {
    //(true) and () - direct, (false) - reverse
    if (flag === undefined) flag = true;
    this.flag = flag;
  }

  encrypt(message, key) {
    if (message === undefined || key === undefined) {
      throw new Error();
    } else {
      let output = "";
      message = message.toUpperCase();
      key = key.toUpperCase();
      let j = 0;
      for (let i = 0; i < message.length; i++) {
        if (message.charCodeAt(i) >= 65 && message.charCodeAt(i) <= 90) {
          output += String.fromCharCode(
            ((message.charCodeAt(i) + key.charCodeAt(j)) % 26) + 65
          );
          j++;
          if (j === key.length) j = 0;
        } else output += message[i];
      }
      if (!this.flag) output = output.split("").reverse().join("");
      return output;
    }
  }
  decrypt(encryptedMessage, key) {
    if (encryptedMessage === undefined || key === undefined) {
      throw new Error();
    } else {
      let output = "";
      encryptedMessage = encryptedMessage.toUpperCase();
      key = key.toUpperCase();
      let j = 0;
      for (let i = 0; i < encryptedMessage.length; i++) {
        if (
          encryptedMessage.charCodeAt(i) >= 65 &&
          encryptedMessage.charCodeAt(i) <= 90
        ) {
          output += String.fromCharCode(
            ((encryptedMessage.charCodeAt(i) - key.charCodeAt(j) + 26) % 26) +
              65
          );
          j++;
          if (j === key.length) j = 0;
        } else output += encryptedMessage[i];
      }
      if (!this.flag) output = output.split("").reverse().join("");
      return output;
    }
  }
}

module.exports = VigenereCipheringMachine;
