Heres my class:
class Cipher(){
constructor(key) {
this.key = !key ? String.fromCharCode(...Array.from({ length: 100 }, () => Math.floor(Math.random() * 26 + 97)),): key;
if (this.key === '' || this.key.match(/[a-z]/g) !== this.key) {
throw new Error('Bad key');
}
}
}
Basically I want:
const foo = new Cipher();
console.log(foo.key);
// random 100 character string
const foo = new Cipher("bar");
console.log(foo.key);
// bar
const foo = new Cipher("Bar")
// [Error 'Bad key']
but every test I've put where 'new Cipher()' is called (an instance with no argument parsed) the code throws an error, basically I want it to recognize that I've already made a random key of 100 lowercase letters and to use that instead...
I think your question is:
If so:
Live Example: