Home > OS >  Find and remove the longest words in a string
Find and remove the longest words in a string

Time:12-19

Write a function whose input parameter is a string. The function should remove the longest words in the line (words-groups of characters separated by spaces, or by a space and the beginning (end) of the string).

At the moment my code is ready and working, but it needs to manually rewrite the methods. In particular, these are .push() (written in the code, but does not work, cannot read property allWords - length), .filter() and .replace(). You cannot use ready-made functions.

Expected Result: 'word wooord wooooooord. woooooord woooord'

function deleteTheLongestWord(str){

    let arrSent = strSplit(str, ".");
    
    let allWords = [];


    let biggestWord = '';

    for (const elem in arrSent) {
        let words = strSplit(arrSent[elem], ' ');
        for (const word in words) {
            allWords = arrPush(allWords, words[word])
        }
    }

    for (const i in allWords) {
        if(biggestWord.length < allWords[i].length){
            biggestWord = allWords[i];
        }
    }

    allWords = allWords.filter(function(e){
        return e.length === biggestWord.length
    })
    for (let i = 0; i < allWords.length; i  ) {
        str = str.replace(" "   allWords[i], '');
    }
    console.log(str);
}

function arrPush(allWords, word){
    allWords[allWords.length] = word
}

function strSplit(str, delimiter){
    const arr = [""];
    const len = delimiter.length;
    let idx = 0;
    for (let i = 0; i < str.length; i  ) {
      let sample = "";
      for (let x = i; x < i   len; x  ) {
        sample  = str[x];
      }
      if (sample === delimiter) {
        i  = len;      
        idx  = 1;      
        arr[idx] = ""; 
      }
      if (str[i]) arr[idx]  = str[i];
    }
    return arr
  }

deleteTheLongestWord('word wooord wooooooooord wooooooord. wooooooooord woooooord woooord wooooooooord');

CodePudding user response:

Your code is failing because of allWords = arrPush(allWords, words[word]); and how arrPush is written.

The first time that arrPush is called, you're passing an empty array allWords and the word to push to that array (second parameter).

But because the function arrPush doesn't return, you're basically unsetting allWords i.e. allWords = undefined. So the code errors as there is no length property of undefined.

The key to solving this, it simply to return allWords; as a result of your arrPush function so that allWords is continually set to the newest (slightly fuller) version of the array.

function deleteTheLongestWord(str){
  
    let arrSent = strSplit(str, ".");
  
    let allWords = [];

    let biggestWord = '';

    for (const elem in arrSent) {
        let words = strSplit(arrSent[elem], ' ');
        for (const word in words) {
          if (words[word] !== ''){
             allWords = arrPush(allWords, words[word]);
          }
        }
    }
  
    for (const i in allWords) {
        if(biggestWord.length < allWords[i].length){
            biggestWord = allWords[i];
        }
    }

    allWords = allWords.filter(function(e){
        return e.length === biggestWord.length
    })
    for (let i = 0; i < allWords.length; i  ) {
        str = str.replace(" "   allWords[i], '');
    }
    console.log(str);
}

function arrPush(allWords, word){
  allWords[allWords.length] = word;
  return allWords;
}

function strSplit(str, delimiter){
    const arr = [""];
    const len = delimiter.length;
    let idx = 0;
    for (let i = 0; i < str.length; i  ) {
      let sample = "";
      for (let x = i; x < i   len; x  ) {
        sample  = str[x];
      }
      if (sample === delimiter) {
        i  = len;      
        idx  = 1;      
        arr[idx] = ""; 
      }
      if (str[i]) arr[idx]  = str[i];
    }
    return arr
}

deleteTheLongestWord('word wooord wooooooooord wooooooord. wooooooooord woooooord woooord wooooooooord');

CodePudding user response:

Here is a short function that does what seems to be expected: removing all the longest words of a string.

const testString =
  "word wooord wooooooooord wooooooord. wooooooooord woooooord woooord wooooooooord";
const expectedResult = "word wooord wooooooord. woooooord woooord";

function deleteTheLongestWord(string) {
  const words = string.split(" ");
  const max = Math.max(...words.map((word) => word.length));
  return words.filter((word) => word.length < max).join(" ");
}

const result = deleteTheLongestWord(testString);
console.log(result);
console.log(result === expectedResult);

And here is the same function in more lines with comments and console logs, so the process can be understood.

const testString =
  "word wooord wooooooooord wooooooord. wooooooooord woooooord woooord wooooooooord";
const expectedResult = "word wooord wooooooord. woooooord woooord";

function deleteTheLongestWord(string) {
  // Get all words in an array
  const words = string.split(" ");
  console.log(words);

  // Create an array containing the length of each words
  const wordsLength = words.map((word) => word.length);
  console.log(wordsLength);

  // Find what the maximum length is
  const max = Math.max(...wordsLength);
  console.log(max);

  // filter the words array keeping only words that are less than the max length
  const result = words.filter((word) => word.length < max);
  console.log(result);

  return result.join(" ");
}

const result = deleteTheLongestWord(testString);
console.log(result);
console.log(result === expectedResult);

CodePudding user response:

For the loop method, you can use split() or otherwise you can use filter().

  • Related