Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can start with this example:

function getStringMultipleOfEight(str) {
  const remainder = str.length % 8;
  const newLength = str.length - remainder;
  return str.substring(0, newLength);
}

console.log(getStringMultipleOfEight('123456789')); // Output: '12345678'
console.log(getStringMultipleOfEight('123456789123')); // Output: '12345678'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading