我如何在JS中写一个函数来return这个单词的缩写?
How do I write a function in JS to return abbreviation of the words?
例如:
makeAbbr('central processing unit') === 'CPU'
我找不到我的错误。感谢您的帮助。
function makeAbbr(words) {
let abbreviation = words[0];
for (let i = 1; i < words.length; i++) {
if (words[i] === '') {
abbreviation += words[i + 1];
}
}
return abbreviation.toUpperCase();
}
console.log(makeAbbr('central processing unit'));
您只需要把words[i] === ''
改成words[i] === ' '
。 ''
是一个空字符串。
另一个选项是拆分传递的字符串。
function makeAbbr(str) {
// words is [ "central", "processing", "unit" ]
let words = str.split(/\s+/);
let abbreviation = '';
for (let i = 0; i < words.length; i++) {
abbreviation += words[i][0];
}
return abbreviation.toUpperCase();
}
这通常适用于使用每个单词的第一个字符由 space 分隔的单词的缩写。
function makeAbbr(words) {
// result abbreviation
let abbreviation = '';
// store each word into an array using split by space
let wordArray = words.split(' ');
// iterate through the word array
for (let i = 0; i < wordArray.length; i++) {
// take the first character in each word into the result abbreviation
abbreviation += wordArray[i][0];
}
// return the abbreviation with all of them being upper case
return abbreviation.toUpperCase();
}
// test case
console.log(makeAbbr('central processing unit'));
const sample = ('central processing unit');
const makeAbrr = (word: string) => {
return word.split(' ').map((letter) => letter[0].toUpperCase()).join('');
}
console.log(makeAbrr(sample));
声明式方法可以是
const makeAbbr = str => str.split(' ').map(i => i.charAt(0).toUpperCase()).join('')
单线解决方案:
function makeAbbr(words) {
return words.split(' ').map(word => word[0].toUpperCase()).join("");
}
console.log(makeAbbr('central processing unit'));
我们正在将 string
句子转换为由 space (.split(" ")
) 分隔的 个单词组成的数组,然后进行映射或转换数组中的每个单词到它的第一个字母,并将其大写:.map(word => word[0].toUpperCase())
,然后将数组元素连接成一个字符串,以“无”""
作为分隔符:
"central processing unit"
-> ["central", "processing", "unit"]
-> ["C", "P", "U"]
-> "CPU"
正则表达式版本
function makeAbbr(text) {
if (typeof text != 'string' || !text) {
return '';
}
const acronym = text
.match(/[\p{Alpha}\p{Nd}]+/gu)
.reduce((previous, next) => previous + ((+next === 0 || parseInt(next)) ? parseInt(next): next[0] || ''), '')
.toUpperCase();
return acronym;
}
console.log(makeAbbr('central processing unit'));
Reference
例如:
makeAbbr('central processing unit') === 'CPU'
我找不到我的错误。感谢您的帮助。
function makeAbbr(words) {
let abbreviation = words[0];
for (let i = 1; i < words.length; i++) {
if (words[i] === '') {
abbreviation += words[i + 1];
}
}
return abbreviation.toUpperCase();
}
console.log(makeAbbr('central processing unit'));
您只需要把words[i] === ''
改成words[i] === ' '
。 ''
是一个空字符串。
另一个选项是拆分传递的字符串。
function makeAbbr(str) {
// words is [ "central", "processing", "unit" ]
let words = str.split(/\s+/);
let abbreviation = '';
for (let i = 0; i < words.length; i++) {
abbreviation += words[i][0];
}
return abbreviation.toUpperCase();
}
这通常适用于使用每个单词的第一个字符由 space 分隔的单词的缩写。
function makeAbbr(words) {
// result abbreviation
let abbreviation = '';
// store each word into an array using split by space
let wordArray = words.split(' ');
// iterate through the word array
for (let i = 0; i < wordArray.length; i++) {
// take the first character in each word into the result abbreviation
abbreviation += wordArray[i][0];
}
// return the abbreviation with all of them being upper case
return abbreviation.toUpperCase();
}
// test case
console.log(makeAbbr('central processing unit'));
const sample = ('central processing unit');
const makeAbrr = (word: string) => {
return word.split(' ').map((letter) => letter[0].toUpperCase()).join('');
}
console.log(makeAbrr(sample));
声明式方法可以是
const makeAbbr = str => str.split(' ').map(i => i.charAt(0).toUpperCase()).join('')
单线解决方案:
function makeAbbr(words) {
return words.split(' ').map(word => word[0].toUpperCase()).join("");
}
console.log(makeAbbr('central processing unit'));
我们正在将 string
句子转换为由 space (.split(" ")
) 分隔的 个单词组成的数组,然后进行映射或转换数组中的每个单词到它的第一个字母,并将其大写:.map(word => word[0].toUpperCase())
,然后将数组元素连接成一个字符串,以“无”""
作为分隔符:
"central processing unit"
-> ["central", "processing", "unit"]
-> ["C", "P", "U"]
-> "CPU"
正则表达式版本
function makeAbbr(text) {
if (typeof text != 'string' || !text) {
return '';
}
const acronym = text
.match(/[\p{Alpha}\p{Nd}]+/gu)
.reduce((previous, next) => previous + ((+next === 0 || parseInt(next)) ? parseInt(next): next[0] || ''), '')
.toUpperCase();
return acronym;
}
console.log(makeAbbr('central processing unit'));
Reference