I am trying to split a string into an array with each iteration of my for-loop. Like if a string is 1234 then I want to split it ['12','34'].
I want to split this string into different ways. Like ['1','2','3','4'], ['123','4'], etc. But I don't know how can I do so?
The string "31173" can be split 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){
// split 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 tried using the split() function but as I learned from the docs that it will use a limit to split the string and return it. So, It won't work like this.
I want to split and check whether the number is prime or not and then push it into the array. So that in the end, I will get the subarrays that contain prime numbers.
How can I split a string and then turn it into an array like this in javascript?
split()as it's meant to take in a specific delimiter. I'd probablypush()asubstring()instead.