英文:
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(['str1', 'str2', 'str3', 'str4'], 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('testing')); // => returns 'TESting'
console.log(capitalizeThreeLetters('again')); // => returns 'AGAin'
//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 < arr.length; i++){
return capitalizeThreeLetters(arr);
}
}
console.log(applyAll(['mai', 'brian', 'jeho', 'han'], capitalizeThreeLetters));
// => returns 'MAIBRIANJEHOhan'
// => should return ['MAI', 'BRIan', 'JEHo', 'HAN']
答案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 = ['mai', 'brian', 'jeho', 'han'].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) => str.slice(0, tot).toUpperCase() + str.slice(tot);
const result = ['mai', 'brian', 'jeho', 'han'].map((str) => capitalize(str, 3));
console.log(result);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论