要打印至少包含4个辅音字母的JavaScript单词,我们可以编写一个简单的程序来检查每个单词是否满足条件。以下是一个示例代码:
function hasAtLeastFourConsonants(word) {
const vowels = 'aeiouAEIOU';
let consonantCount = 0;
for (let char of word) {
if (!vowels.includes(char) && char.match(/[a-zA-Z]/)) {
consonantCount++;
}
}
return consonantCount >= 4;
}
function printWordsWithFourConsonants(words) {
for (let word of words) {
if (hasAtLeastFourConsonants(word)) {
console.log(word);
}
}
}
const words = ['javascript', 'programming', 'algorithm', 'consonant', 'example'];
printWordsWithFourConsonants(words);
hasAtLeastFourConsonants
函数检查每个单词,如果满足条件,则打印该单词。这个示例展示了如何使用JavaScript来处理和分析字符串数据,这是编程中常见的任务之一。
领取专属 10元无门槛券
手把手带您无忧上云