My goal is to break a string into an array during each iteration of my for-loop. For example, if the string is "1234", I want it split as ['12', '34']."
I am looking to explore different ways of splitting this string such as ['1', '2', '3', '4'], ['123', '4'], etc. However, I'm unsure how to achieve this.
The string "31173" can be divided into prime numbers in 6 ways:
[3, 11, 7, 3]
[3, 11, 73]
[31, 17, 3]
[31, 173]
[311, 7, 3]
[311, 73]
let k=1;
for(let i=0; i<inputStr.length; i++){
if(k<inputStr.length){
// splitting the string
let splittedNums=inputStr.split('',i+k);
for(let j=0; j<splittedNums.length; j++){
if(isPrime(splittedNums[j]))
result.push([splittedNums[j]]);
}
}
k++;
}
I attempted to use the split() function but found from the documentation that it has limitations when splitting and returning the string, making it unsuitable for my requirements.
My aim is to split the string, check if the resulting number is prime, and then store it in an array. Ultimately, I intend to gather subarrays containing only prime numbers.
How can I efficiently split a string and convert it into an array like this in JavaScript?