encryption

    0

    0

    The encryption function takes a string and creates an encrypted string by using a cryptographic algorithm. The ceil function rounds down the length of the string to an integer. The substring function takes the first n characters of the string, converts them to a number and returns it. The reduce function takes two arguments: an iterator and a function that takes two values and returns a single value. The function returns the value of r + (v[i] || ""). The function calls the array.reduce() function with the value of r and the value of v[i] as arguments.

    function encryption(s) {
      const ceil = Math.ceil(Math.sqrt(s.length));
    
      let temp = s;
      let array = [];
    
      while (temp) {
        array = array.concat(temp.substring(0, ceil));
        temp = temp.substring(ceil);
      }
    
      let result = [];
    
      for (let i = 0; i < ceil; i++) {
        result = result.concat(
          array.reduce((r, v) => {
            return r + (v[i] || "");
          }, "")
        );
      }
      return result.join(" ");
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.