将给定数组中每个单词的前三个字母大写。

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

Function to capitalize first three letters on each word within given array

问题

我编写了一个函数,它接受一个单词并将前三个字母大写。现在我需要在一个单词数组上运行相同的函数,以便它返回每个单词的前三个字母大写。我看到有很多人问如何将句子中每个单词的第一个字母大写,但这不是同一件事。我必须使用我已经编写的函数,所以当我使用console.log打印它时,它看起来像这样:

// 在数组上应用capitalizeThreeLetters函数
console.log(applyAll(['str1', 'str2', 'str3', 'str4'], capitalizeThreeLetters));

我尝试使用for循环来一起返回每个单词。在我的研究中,我看到了forEach()方法可以用于在数组元素上运行函数,但我无法弄清楚如何应用它。

// 接受字符串并返回前三个字母大写的函数
function capitalizeThreeLetters(str){
  let capFirst = str[0].toUpperCase();
  let capSecond = str[1].toUpperCase();
  let capThird = str[2].toUpperCase();
  let splitStr = str.slice(3);
  let wholeStr = capFirst + capSecond + capThird + splitStr;
  return wholeStr;
}

console.log(capitalizeThreeLetters('testing')); // => 返回 'TESting'
console.log(capitalizeThreeLetters('again')); // => 返回 'AGAin'

// 接受字符串数组并对每个数组元素应用capitalizeThreeLetters函数,以便每个单词的前三个字母都大写
function applyAll(arr){
  let result = [];
  for (let i = 0; i < arr.length; i++){
    result.push(capitalizeThreeLetters(arr[i]));
  }
  return result;
}  

console.log(applyAll(['mai', 'brian', 'jeho', 'han'])); 
// => 返回 ['MAI', 'BRIan', 'JEHo', 'HAN']
英文:

I wrote a function that takes in a single word and capitalizes the first three letters. Now I need to run this same function on an array of words, so that it returns each word with the first three letters capitalized. I see there's a lot of people asking how to capitalize the first letter on each word of a sentence, but this is not the same thing. I have to use the function I already wrote so when I console.log it it looks like this:

console.log(applyAll([&#39;str1&#39;, &#39;str2&#39;, &#39;str3&#39;, &#39;str4&#39;], capitalizeThreeLetters));

I tried doing this using a for loop which returns every word together. In my research I saw the forEach() method could be used to run a function on array elements but I cannot figure out how to apply it.


//Function that takes in str returns it with three first letters capitalized

function capitalizeThreeLetters(str){

  let capFirst = str[0].toUpperCase();
  let capSecond = str[1].toUpperCase();
  let capThird = str[2].toUpperCase();

  let splitStr = str.slice(3);

  let wholeStr = capFirst + capSecond + capThird + splitStr;
  return wholeStr;
}

console.log(capitalizeThreeLetters(&#39;testing&#39;)); // =&gt; returns &#39;TESting&#39;
console.log(capitalizeThreeLetters(&#39;again&#39;)); // =&gt; returns &#39;AGAin&#39;



//Function that takes in a string array and applies capitalizeThreeLetters function to each array element so each word is returned with first three letters capitalized

function applyAll(arr){
  
  for (let i = 0; i &lt; arr.length; i++){
  return capitalizeThreeLetters(arr);
  }
}  

console.log(applyAll([&#39;mai&#39;, &#39;brian&#39;, &#39;jeho&#39;, &#39;han&#39;], capitalizeThreeLetters)); 
// =&gt; returns &#39;MAIBRIANJEHOhan&#39;
// =&gt; should return [&#39;MAI&#39;, &#39;BRIan&#39;, &#39;JEHo&#39;, &#39;HAN&#39;]

答案1

得分: 1

Your applyAll function doesn't work as intended. When you return, the function exits immediately, which stops the loop from continuing and running the other 2 iterations.

Your second issue is that you pass the entire array to capitalizeThreeLetters instead of a single word. You probably want capitalizeThreeLetters(arr[i]). Currently, you are passing the entire array, causing the first 3 words in the array to be capitalized instead of the first 3 letters of each word.

You can fix it using map:

function applyAll(arr){
  return arr.map(capitalizeThreeLetters);
}

The map function on an array will call a function on each element of the array and return a new array with the results.

英文:

Your applyAll function doesn't work the way you want. When you return the function immediately exits. In this case, you return inside the loop thus prevent the loop from continuing and running the other 2 iterations.

Your second issue is that you pass the entire array to capitalizeThreeLetters instead of a single word. You probably want capitalizeThreeLetters(arr[i]). Right now, you are passing the entire array in which leads to the first 3 words in the array being capitalized instead of the first 3 letters of each word.

You can fix it using map:

function applyAll(arr){
  return arr.map(capitalizeThreeLetters);
}

The map function on an array will call a function on each element of the array then return a new array with the results.

答案2

得分: 0

使用Array.prototype.map来获取一个新数组,其中包含当前数组的修改后的值。

// 以下是第一个示例的翻译
// 函数接受一个字符串并返回前三个字母大写的版本

function capitalizeThreeLetters(str) {

  let capFirst = str[0].toUpperCase();
  let capSecond = str[1].toUpperCase();
  let capThird = str[2].toUpperCase();

  let splitStr = str.slice(3);

  let wholeStr = capFirst + capSecond + capThird + splitStr;
  return wholeStr;
}

const result = ['mai', 'brian', 'jeho', 'han'].map(capitalizeThreeLetters);

console.log(result);

// 以下是第二个示例的翻译
// 不需要将每个 `[0] [1]` 等字符转换为大写,您可以在切片部分上使用 `.toUppercase()`。
// 您还可以将要大写的字符数参数化:

const capitalize = (str, tot=1) => str.slice(0, tot).toUpperCase() + str.slice(tot);

const result = ['mai', 'brian', 'jeho', 'han'].map((str) => capitalize(str, 3));
console.log(result);

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
英文:

Use Array.prototype.map to get a new array with the modified values of a current one

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

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

//Function that takes in str returns it with three first letters capitalized

function capitalizeThreeLetters(str){

  let capFirst = str[0].toUpperCase();
  let capSecond = str[1].toUpperCase();
  let capThird = str[2].toUpperCase();

  let splitStr = str.slice(3);

  let wholeStr = capFirst + capSecond + capThird + splitStr;
  return wholeStr;
}

const result = [&#39;mai&#39;, &#39;brian&#39;, &#39;jeho&#39;, &#39;han&#39;].map(capitalizeThreeLetters);

console.log(result); 

<!-- end snippet -->

There's no need to convert each [0] [1] etc characters to uppercase, you can use .toUppercase() on the sliced portion.
Also you can parametrize the number of characters to capitalize:

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

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

const capitalize = (str, tot=1) =&gt; str.slice(0, tot).toUpperCase() + str.slice(tot);

const result = [&#39;mai&#39;, &#39;brian&#39;, &#39;jeho&#39;, &#39;han&#39;].map((str) =&gt; capitalize(str, 3));
console.log(result); 

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月18日 15:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710609.html
匿名

发表评论

匿名网友

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

确定