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));
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'));
5条答案
按热度按时间sg3maiej1#
这里还有一个例子供你参考
5n0oy7gb2#
您可以从以下示例开始:
daupos2t3#
希望这是你想要的
pkbketx94#
尽管你还没有发布任何你自己的尝试来解决这个问题,这里有一个开始:
Remainder (%)运算符派上了用场。
1.获取字符串的完整长度
1.应用余数运算符以获取过长的字符串长度
1.把绳子从末端取下来
eoxn13cs5#
我解决了我自己的问题,答案是我们只需要从字符串的长度中减去余数,例如return str.slice(0,str. length-str.length%8)