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
CodePudding user response:
Here is another example for your reference
var a = '1234567891231234567891236';
console.log(a.substring(8*Math.floor(a.length/8), 0));
CodePudding user response:
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'
CodePudding user response:
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));
CodePudding user response:
Even though you haven't posted any of your own attempts to solve this, here's a starter:
The Remainder (%) operator comes in handy.
- Get the full length of the string
- apply the remainder operator in order to get the length of string that is too much
- remove the string from the end
function returnStringMultipleEight(originalString) {
// get the length which should be removed
const lengthTooMuch = originalString.length % 8;
// calculate the length from first character to the last one to keep
const lengthToEight = originalString.length - lengthTooMuch;
// get the substring
const reducedString = originalString.substring(0, lengthToEight);
return reducedString;
}
console.log(returnStringMultipleEight('123456789123'));
console.log(returnStringMultipleEight('12345678912345678912345'));
console.log(returnStringMultipleEight('askldjaflskjflskdfj'));
CodePudding user response:
Hello stack community I Solved my own question the answer is that we just subtract the remainder from the length of the string for instance return str.slice(0,str.length-str.length%8)