长度最长的所有字符串

时间:2018-10-14 20:18:07

标签: javascript string

嘿,我有这个功能:

function frame(string) {
  var words = string.split(" ");
  var longestWord = 0;
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord) {
      longestWord = words[i].length;
    }
  }
  console.log(y.repeat(longestWord + 4));
  for (var i = 0; i < words.length; i++) {
    console.log(y + " " + words[i] + " " + y);
    //words[i].length = longestWord;
  }
  console.log(y.repeat(longestWord + 4));
}

我希望所有字符串的长度与最长的字符串相同。其余的一切都很好。被注释掉的代码是我上次尝试的代码,但无法正常工作。 显然,逻辑并不完全正确,但是我缺少了什么? 谢谢

6 个答案:

答案 0 :(得分:2)

字符串在JS中是不可变的,您不能只覆盖length属性;)

您的意思是,您需要在单词之后填充字符,直到最长的单词的长度?如果我正确理解,请尝试执行以下操作:

console.log(y + " " + words[i].padEnd(longestWord, " ") + " " + y);

这似乎是最简单的方法,但如果涵盖了所有浏览器,请咨询Can I use

答案 1 :(得分:2)

我很想摆脱一些for循环以提高可读性。您可以使用Math.maxmap查找最长的单词,并使用padEnd确定长度:

function frame(string, y = '*') {
  var words = string.split(" ");
  let longestlength = Math.max(...words.map(w => w.length))
  let header = y.repeat(longestlength + 4)
  return [
    header,
    ...words.map(word => `${y} ${word.padEnd(longestlength, ' ')} ${y}`),
    header
  ]
}

console.log(frame("hello the word javascript is long").join('\n'))

答案 2 :(得分:1)

计算剩余的空间,然后重复以下操作。

function frame(string) {
  var words = string.split(" ");
  var longestWord = 0;
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord) {
      longestWord = words[i].length;
    }
  }
  console.log(y.repeat(longestWord + 4));
  for (var i = 0; i < words.length; i++) {
    spaceLeft = longestWord - words[i].length;
    console.log(y + " " + words[i] + " ".repeat(spaceLeft) + " " + y);
    //words[i].length = longestWord;
  }
  console.log(y.repeat(longestWord + 4));
}

frame('skfj dslfska sadflkdsflkdsnf ldsknflskdnaf dslkf')

答案 3 :(得分:1)

查找单词最大长度的第一个循环似乎在第二个循环中正确,您可以使用if语句 您所要做的就是 首先定义一个数组 让a = [] 在注释部分的第二个循环中,写

if(word[I].length == longestlength){a.push(word[I])} 

此后,您将得到包含长度等于最大长度的单词的数组 希望对您有帮助

答案 4 :(得分:0)

function frame(string) {
  var words = string.split(" ");
  var longestWord = 0;
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord) {
      longestWord = words[i].length;
    }
  }
  console.log(y.repeat(longestWord));
  for (var i = 0; i < words.length; i++) {
    console.log(words[i] + y.repeat(longestWord - words[i].length));
    //words[i].length = longestWord;
  }
}

frame('Lorem ipsum dolor sit amet, consectetur adipiscing elit')

也许是这样,现在每个单词的长度与最长的单词相同,空白处充满*

答案 5 :(得分:0)

查看内联评论:

function frame(string) {
  var words = string.split(" ");
  var longestWord = "";
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord.length) {
      longestWord = words[i];
    }
  }
  console.log("Longest word is: " + longestWord)
  console.log(y.repeat(longestWord.length + 4));
  for (var i = 0; i < words.length; i++) {
    // Get the difference between the word and the longest word
    var diff = longestWord.length - words[i].length;
      // Pad the words with the right amount of spaces to make up the difference
      console.log(y + " " + words[i] + " ".repeat(diff) + y);
  }
  console.log(y.repeat(longestWord.length + 4));
}

frame("The quick brown fox jumped over the lazy dog.");