Home > OS >  Implement the .split method
Implement the .split method

Time:12-19

I need to implement the .split method in my own way without using prebuilt functions. The method should receive a string divided into 2 sentences by a dot and divide them through a separator. For example, there is this string: 'word wooord wooooooooord wooooooord. wooooooooord woooooord woooord wooooooooord', separator in this case: '. ' The result should be: ['word wooord wooooooooord wooooooord", "wooooooooord woooooord woooord wooooooooord']

I tried to implement it myself, the first problem I encountered is that the words from the string are added character by character to the new array. The second problem is that the output is still a string even though I declared an array earlier.

function split(str, splitter){
    let arrSent = []
    for (let i = 0; i < str.length; i  ){
        if (str[i] != splitter){
            arrSent  = str[i]
        }
    }
    return arrSent
}
console.log(split('word wooord wooooooooord wooooooord. wooooooooord woooooord woooord wooooooooord', '. '))

CodePudding user response:

Since the delimiter can have more than one character, you need a system to upfront collect a sample of characters (of the same length as the delimiter) to be then compared with the delimiter:

const split = (str, delimiter) => {

  // If delimiter is empty string just return an array of characters
  if (delimiter === "") return [...str];

  const len = delimiter.length;
  const iter = str.length - len   1; // max needed iterations
  const arr = [""]; // Prefill it with empty string
  let idx = 0; // arr insertion pointer
  
  for (let i = 0; i < iter; i  ) {
  
    // Collect len chars from str as a sample
    // to compare with the delimiter:
    let sample = "";
    for (let x = i; x < i   len; x  ) {
      sample  = str[x];
    }
    
    const isSplit = sample === delimiter;
    const isEnded = i === iter - 1;

    if (isSplit) {
      i  = len - 1;  // Consume splitted characters
      idx  = 1;      // Increment arr pointer
      arr[idx] = ""; // Prepare the new array key as empty string
    } else {
      // If loop ended append the entire sample.
      // Otherwise, append a single character:
      arr[idx]  = isEnded ? sample : str[i];
    }
  }
  
  return arr
}

console.log(split("word. etc", ". "));
console.log(split("word. etc. ", ". "));
console.log(split(". word yep. . etc. ", ". "));
console.log(split("word", ". "));
console.log(split("word", "word"));
console.log(split("word", ""));
console.log(split("", ""));

above, idx (starting at 0) is used as the output's arr insertion pointer. The idx is incremented if the sample matches the delimiter. Also, if there's a match, we need to skip iterations i = len, to not include the delimiter in the output array.

To test, create many examples and right before return arr; use console.log(JSON.stringify(arr) === JSON.stringify(str.split(delimiter))); - it should return true for all the submitted tests.

  • Related