我正在做一个问题,我必须将提供的字符串中每个单词的第一个字母大写。

huangapple go评论90阅读模式
英文:

I am doing a problem that I have to capitalize the first letter of each word in a provided string

问题

function generateHashtag(str) {
  const words = str.split(" ");
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }
  return words.join("");
}
console.log(generateHashtag("i am a good coder"))

这是我想出来的代码,但在迭代字符串后,输出的是一个具有首字母大写的单词的数组。如何将这些单词连接起来,形成没有空格的句子?

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function generateHashtag(str) {
  const words = str.split(&quot; &quot;);
  for (let i = 0; i &lt; words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }
  words.join(&quot; &quot;);

  return words
}
console.log(generateHashtag(&quot;i am a good coder&quot;))

<!-- end snippet -->

This is the code I have come up with but after iterating the string, the output is an array with the first words capitalized. How do I join up the words from the array to form back the sentence without the spaces?

答案1

得分: 1

Array#join() 返回一个新的 string。直接返回该值,而不是在没有赋值的情况下在 words 上调用它。

function generateHashtag(str) {
  const words = str.split(" ");
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }

  return words.join(" ");
}

console.log(generateHashtag("i am a good coder"));

来自 developer.mozilla.org

join() 方法通过使用逗号或指定的分隔符字符串连接数组(或类似数组的对象)中的所有元素创建并返回一个新字符串。如果数组只有一个项,则不使用分隔符将返回该项。

英文:

Array#join() returns a new string. Return that value directly instead of calling it on words without assignment.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function generateHashtag(str) {
  const words = str.split(&quot; &quot;);
  for (let i = 0; i &lt; words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }

  return words.join(&quot; &quot;);
}

console.log(generateHashtag(&quot;i am a good coder&quot;));

<!-- end snippet -->

From developer.mozilla.org

> The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

答案2

得分: 1

words.join() 创建一个新值,它不会更新 words 的值。

尝试这样做:

function generateHashtag(str) {
  const words = str.split(" ");
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }

  return words.join(' ')
}
console.log(generateHashtag("i am a good coder"))

在这里,我们返回 words.join(' ')。

英文:

words.join() creates a new value, it doesn't update the words value.

Try this:

function generateHashtag(str) {
  const words = str.split(&quot; &quot;);
  for (let i = 0; i &lt; words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }

  return words.join(&#39; &#39;)
}
console.log(generateHashtag(&quot;i am a good coder&quot;))

Here we return words.join(' ')

huangapple
  • 本文由 发表于 2023年8月4日 22:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定