Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
0


I want to remove the single string and want to keep string with minimum length 3 and above i tried to access string with this if(result.string >= 3) but it is giving array length so i tried to access string but i cant. so please anybody who can help me


What I have tried:

let stringCombinations = (str) => {
    let strLength = str.length;
    let result = [];
    let currentIndex = 0;
    while (currentIndex < strLength) {
      let char = str.charAt(currentIndex);
      let x;
      let arrTemp = [char];
      for (x in result) {
        arrTemp.push("" + result[x] + char);
    }
      result = result.concat(arrTemp);
      currentIndex++;
    }
    return result;
  };
  console.log(stringCombinations("BLADE"));

This is my ouput
output: (31) ["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]


This is what I want
["BLA", "BLD", "BAD", "LAD", "BLAD", "BLE", "BAE", "LAE", "BLAE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]"
Posted
Updated 6-Jul-21 0:27am
Comments
Patrice T 5-Jul-21 15:28pm    
And you have a question or a problem ?
Happy kaul 5-Jul-21 15:34pm    
Both
Patrice T 5-Jul-21 16:12pm    
And you plan to describe them ?
Richard MacCutchan 6-Jul-21 3:37am    
What is the structure of the input data?
Happy kaul 6-Jul-21 3:40am    
Input is "blaze"

But you can put any input. it will give combination of that word but with length minimum of 3 and can be more than 3

I don't know Javascript but have a sense of what your code is doing. You're finding all combinations except for the empty string, which is a good start. But I see nothing in your code that filters out combinations of less than 3 letters. It looks like you build on such combinations, so it would probably be easiest to fix the code by removing them before you return result.
 
Share this answer
 
Replace your
JavaScript
return result;
with
JavaScript
return result.filter(x => x.length >= 3);
Because the way you generate the combinations, you can't filter short combinations earlier.

Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900