javascript [1,2,3,4,5,6,7] becomes 123, 234, 345, 456 etc
The simplest solution will be to loop over the array and loop until length - 2
as:
const num = 1234567;
const strNum = String(num);
const result = [];
for (let i = 0; i < strNum.length - 2; ++i) {
result.push(strNum.slice(i, i + 3));
}
console.log(result);
You can even create a generic function and get the result string of desired length
const num = 1234567;
const strNum = String(num);
function getPartialString(str, len) {
const result = [];
for (let i = 0; i < strNum.length - (len - 1); ++i) {
result.push(strNum.slice(i, i + len));
}
return result;
}
console.log(getPartialString(strNum, 3));
console.log(getPartialString(strNum, 4));
console.log(getPartialString(strNum, 5));
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }