NodeJS 查找并删除字符串中最长的单词

eoxn13cs  于 2022-12-22  发布在  Node.js
关注(0)|答案(3)|浏览(140)

编写一个输入参数为字符串的函数。该函数应删除行中最长的单词(单词-由空格分隔的字符组,或由空格和字符串的开头(结尾)分隔的字符组)。
目前我的代码已经准备就绪,可以正常工作,但需要手动重写方法。特别是. push()(写在代码中,但不起作用,无法读取属性allWords-length)、. filter()和. replace()。您不能使用现成的函数。
预期结果:"字呜呜呜呜呜呜呜呜呜呜"

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');
0wi1tuuw

0wi1tuuw1#

由于allWords = arrPush(allWords, words[word]);arrPush的编写方式,您的代码失败。
第一次调用arrPush时,传递一个空数组allWords和要推送到该数组的字(第二个参数)。
但是因为函数arrPush没有返回,你基本上是在取消设置allWords,也就是allWords = undefined,所以代码错误是因为没有undefinedlength属性。
解决这个问题的关键是,只需将return allWords;作为arrPush函数的结果,以便allWords不断设置为数组的最新(稍微更完整)版本。

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');
kxe2p93d

kxe2p93d2#

下面是一个简短的函数,它完成了预期的任务:删除字符串中所有最长的单词。

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);

这里是同一个函数,但有更多行注解和控制台日志,因此可以理解这个过程。

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);
cyvaqqii

cyvaqqii3#

对于loop方法,可以使用split(),也可以使用filter()。

相关问题