Search code examples
javascriptstringlogic

I want a string length multiple of 8 and remove any extra digits in the string


I want a string to return a length of multiple of 8 for example if I've 123456789 so here I need to return a string 12345678 removing the last digit Example 2: for instance string length is 123456789123=>here it should remove 9123 and return me 1234 5678

I'm returning a string length when I pass the length of string to be 8 the length that are not multiple of 8 I should make them multiple of 8 by removing the exessive characters


Solution

  • Hope it is what you want

    let str = "1234567887654321deleted"
    
    function get8Multiple(tobehandlestring){
      let len = str.length
      if (len <= 8) return tobehandlestring;
      
      let extra = len % 8;
      
      return tobehandlestring.substring(0, len-extra);
      
    }
    
    console.log(get8Multiple(str));