momo3141

matchNames

Feb 18th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Match{
  2.    #inPut = [];
  3.    #outPut = {};
  4.    constructor (...data){
  5.       this.#inPut = [...data];
  6.       if (this.#inPut.length%2 !== 0){
  7.          throw new Error ('Enter even number of names')
  8.       }
  9.    }
  10.    get match () {
  11.       while(this.#inPut.length !== 0){
  12.          let indexfOfkey = Math.floor(this.#inPut.length*Math.random());
  13.          let key = this.#inPut.splice(indexfOfkey,1);
  14.          let indexOfvalue = Math.floor(this.#inPut.length*Math.random());
  15.          let value = this.#inPut.splice(indexOfvalue,1);
  16.          this.#outPut[key] = value
  17.  
  18.       }
  19.       console.table(this.#outPut);
  20.    }
  21. }
  22. const names = new Match('ivo','gosho','mitko','stoyan','nikolay','atanas','martin','ivaylo','krystan','grigoriy')
  23. names.match
Advertisement
Add Comment
Please, Sign In to add comment