Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将每个单词的首字母和末字母大写的函数不起作用_Javascript - Fatal编程技术网

Javascript 将每个单词的首字母和末字母大写的函数不起作用

Javascript 将每个单词的首字母和末字母大写的函数不起作用,javascript,Javascript,我创建了一个函数,给定任何字符串,该函数将返回每个单词的首字母和末字母大写的字符串。到目前为止,它在某些词上有效,在其他词上无效,有人能帮我找出原因吗 function Capitalize(str) { var spl = str.split(" "); var words = []; for (let i = 0; i < spl.length; i++) { //For every word for (let j = 0; j < spl[i].l

我创建了一个函数,给定任何字符串,该函数将返回每个单词的首字母和末字母大写的字符串。到目前为止,它在某些词上有效,在其他词上无效,有人能帮我找出原因吗

function Capitalize(str) {
  var spl = str.split(" "); 
  var words = [];
  for (let i = 0; i < spl.length; i++) {
    //For every word
    for (let j = 0; j < spl[i].length; j++) {
      //For every letter in each word
      var word = spl[i];
      var size = spl[i].length;
      var firstLetterCapital = word.replace(word[0], word[0].toUpperCase()); //Creates new array
      var LastLetterCapital = firstLetterCapital.replace(
        word[size - 1],
        word[size - 1].toUpperCase()
      );
    }
    words.push(LastLetterCapital);
  }
  console.log(words.join(" "));
}

Capitalize("hello there");
函数大写(str){
var spl=str.split(“”);
var-words=[];
for(设i=0;i
当我键入:
Capitalize(“我的名字是约翰·史密斯”)
时,它会起作用,但在键入
Capitalize(“您好”)


我知道这完全是一团糟,可能是一种非常糟糕的方式,但我一个月前就开始编程了,所以让我休息一下:)

试试这个,它对hello world有效,因为我想你希望结果是hello,对吧

  function capitalize(str) {
  var spl = str.split(" "); 
  var words = [];
  for (let i = 0; i < spl.length; i++) {
    //For every word

    let changedWord = "";
    for (let j = 0; j < spl[i].length; j++) {
      //For every letter in each word
      if(j == 0 || j == spl[i].length - 1) {
        changedWord += spl[i][j].toUpperCase();
      } else {
        changedWord += spl[i][j].toLowerCase();
      }

    }
    words.push(changedWord);
    console.log(words);
  }
  console.log(words.join(" "));
}

capitalize("hello there");
函数大写(str){
var spl=str.split(“”);
var-words=[];
for(设i=0;i

另外:使函数名以小写字母开头。事情就是这样。以大写字母开头通常是类。一个简单的提示

您的代码无法工作的原因是使用了
replace()
<代码>替换()
将始终替换找到的第一个字符

绝对没有理由运行嵌套循环。您可以使用单个循环来实现这一点

功能帽(str){
设spl=str.split(“”);
for(设i=0;iconsole.log(cap(“一只敏捷的棕色狐狸”)
这应该可以做到:

函数大写(str){
返回str.replace(/(\b\w |\w\b)/g,l=>l.toUpperCase())
}

log(大写(“我想以一种非常奇怪的方式大写”)
祝贺您开始编程

你可以用它来实现你想做的事情

function capitalizeFirstAndLastLetters (str) {
    const words = str.split(" "); // Split the string into words
    const modified = [];

    for (const word of words) {
        if (word.length <= 2) {
            modified.push(word.toUpperCase()); // If the word less than 3 characters, the whole word is capitalized
            continue;
        }

        var firstCapital = word[0].toUpperCase(); // word[0] gets the first index of the string (I.e. the first letter of the word)
        var lastCapital = word.slice(-1).toUpperCase(); // The slice function slices a portion of the word. slice(-1) gets the last letter
        var middlePart = word.slice(1, -1); // slice(1, -1) means start slicing from the second index (I.e. 1) and ignore the last index

        modified.push(firstCapital + middlePart + lastCapital);
    }

    return modified.join(" "); // Join each element in the modified array with a space to get the final string with each words first and last letters capitalized
}

capitalizeFirstAndLastLetters("hello there I am a boy"); // "HellO TherE I AM A BoY"
函数首字母大写和末字母大写(str){
const words=str.split(“”;//将字符串拆分为单词
修改常数=[];
for(const单词中的单词){
如果(word.length此表达式:

var LastLetterCapital = firstLetterCapital.replace(
    word[size - 1],
    word[size - 1].toUpperCase()
  );
正在将“There”中第一个出现的字符“e”替换为大写的“e”

解释
replace()
函数首先将第一个参数:
word[size-1]
转换为文字字符“e”,然后用大写字母“e”替换该字符的第一个匹配项,生成字符串“ThEre”

解决方案 改用正则表达式作为第一个参数,以确保最后一个字符是目标字符,而不管该字符是否显示在单词中的其他任何位置:

  var LastLetterCapital = firstLetterCapital.replace(/.$/, word[size - 1].toUpperCase());
函数大写(str){
var spl=str.split(“”);
var-words=[];
for(设i=0;i大写(“您好”);
也许这就是您想要的,但不想对您的代码做太多更改:

函数大写(str){
var spl=str.split(“”);
var-words=[];
for(设i=0;i大写(“hello there”);
为了简化函数,可以将字符串转换为数组,将每个单词转换为所需的格式,然后再将其合并为字符串

函数大写(str){
返回str.split(“”).map((word)=>word.charAt(0.toUpperCase()+
(word.length>2?word.substring(1,word.length-1):“”)+
(word.length>1?word.charAt(word.length-1.toUpperCase():“”)。join(“”);
}

console.log(大写(“我想大写第一个字母和最后一个字母”);
字符串类型是不可变的,所以为什么不尝试将字符串转换为数组,如y=word.split(“”)和do y[0]=word.charAt(0)。toUpperCase()然后用y.join(“”)转换回字符串

@symlink已经解释了为什么它是“HellO here”他也给出了一个解决方案,明确地将字符串的第一个和最后一个字符作为目标。我完成的工作与成员们已经发布的工作没有太大的不同,除了..“maybe”稍微多解释一下

您可以通过这四个步骤来解决整个问题

  • 将所有单词放入一个数组中
  • 创建一个函数,它接受每个单词和tar