有没有一种方法可以使用for循环来识别数组中的特定变量?

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

Is there a way to identify certain variables in an array using a for loop?

问题

我需要开发一个代码,该代码会遍历数组中的每个选项,同时计算每个选项中的元音和辅音数量。这也需要在控制台中打印出来。

let vowels = 0;
let consonants = 0;
const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];

for (let fruit of fruits) {
console.log(fruit);
for (let character of fruit.toLowerCase()) {
if ("aeiou".includes(character)) {
vowels += 1;
} else if (character >= "a" && character <= "z") {
consonants += 1;
}
}
console.log(Vowels: ${vowels}, Consonants: ${consonants});
vowels = 0;
consonants = 0;
}


这是我目前的代码,有人可以解释一下我漏掉了什么吗?
当我在Visual Studio Code中运行它时,控制台仍然显示数组中的所有选项,而不记录变量的元音或辅音是否被处理或增加了:

编辑:我需要计算数组中的每个单词,并显示每个单词中有多少元音和辅音。

[运行] node "d:\coding.js"
Apple
Vowels: 2, Consonants: 3
Orange
Vowels: 3, Consonants: 3
Pear
Vowels: 2, Consonants: 2
Peach
Vowels: 3, Consonants: 2
Strawberry
Vowels: 3, Consonants: 7
Cherry
Vowels: 2, Consonants: 4
Acai
Vowels: 3, Consonants: 1


<details>
<summary>英文:</summary>

I have to develop code that reads through each option in an array while counting the vowels and consonants in each option. This also needs to be printed in the console.

let vowels = 0;
let consonants = 0;
const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];

for (let fruit in fruits) {
console.log(fruits[fruit]);
if (fruits[fruit] == "a" || fruits[fruit] == "e" || fruits[fruit] == "i" || fruits[fruit] == "o" || fruits[fruit] == "u") {
vowels + 1;
}
else {
consonants + 1;
}
}


This is what I have so far, can someone explain to me what I am missing?
Once I ran it in Visual Studio Code, the console still showed all the options in the array without registering if the variable&#39;s Vowels or Consonants were even being addressed or increased:

Edit: I need to count each word in the array and display how many vowels and consonants are in each word.


[Running] node "d:\coding.js"
Apple
Orange
Pear
Peach
Strawberry
Cherry
Acai

[Done] exited with code=0 in 0.128 seconds



</details>


# 答案1
**得分**: 1

我已经为您的代码添加了一些额外的 console.logs,并且使用了另一个变量 `letter`。

你想要查看每个单词中的一个字母,还是需要计算每个单词中的每个字母?

```javascript
let vowels = 0;
let consonants = 0;
const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];

for (let word of fruits) {
    console.log("working on word", word);

    letter = word[0];   // 这可能是错误的,你需要检查单词的每个字母吗?

    console.log("  looking at letter", letter);

    if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u") {
        vowels + 1;
    }
    else {
        consonants + 1;
    }
    console.log("  up to now i found", vowels, "vowels and", consonants, "consonants");
}

你写道你需要计算每个字母,所以我们需要另一个循环来遍历当前单词的每个字母:

let vowels = 0;
let consonants = 0;
const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];

for (let word of fruits) {
    console.log("working on word", word);
    
    for (let letter of word.split('')) {

      console.log("  looking at letter", letter);

      if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u") {
          vowels += 1;
      }
      else {
          consonants += 1;
      }
      console.log("  up to now i found", vowels, "vowels and", consonants, "consonants");
    
    }
}

这仍然不会计算大写字母。但我相信你可以修复这个问题。

英文:

I've added some more console.logs to you code,
and I've used another variable letter.

Do you want to look at just one letter per word,
or do you need to count every letter in every word?

    let vowels = 0;
    let consonants = 0;
    const fruits = [&quot;Apple&quot;, &quot;Orange&quot;, &quot;Pear&quot;, &quot;Peach&quot;, &quot;Strawberry&quot;, &quot;Cherry&quot;, &quot;Acai&quot;];
    
    for (let word of fruits) {
        console.log(&quot;working on word&quot;, word);
    
        letter = word[0];   // this is probably wrong, do you need to check EVERY letter of a word?
    
        console.log(&quot;  looking at letter&quot;, letter);
    
        if (letter == &quot;a&quot; || letter == &quot;e&quot; || letter == &quot;i&quot; || letter == &quot;o&quot; || letter == &quot;u&quot;) {
            vowels + 1;
        }
        else {
            consonants + 1;
        }
        console.log(&quot;  up to now i found&quot;, vowels, &quot;vowels and&quot;, consonants, &quot;consonants&quot;);
    }

You wrote that you need to count
every letter, so we need another loop to go through
each letter of the current word:

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

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

        let vowels = 0;
        let consonants = 0;
        const fruits = [&quot;Apple&quot;, &quot;Orange&quot;, &quot;Pear&quot;, &quot;Peach&quot;, &quot;Strawberry&quot;, &quot;Cherry&quot;, &quot;Acai&quot;];
        
        for (let word of fruits) {
            console.log(&quot;working on word&quot;, word);
            
            for (let letter of word.split(&#39;&#39;)) {

              console.log(&quot;  looking at letter&quot;, letter);

              if (letter == &quot;a&quot; || letter == &quot;e&quot; || letter == &quot;i&quot; || letter == &quot;o&quot; || letter == &quot;u&quot;) {
                  vowels += 1;
              }
              else {
                  consonants += 1;
              }
              console.log(&quot;  up to now i found&quot;, vowels, &quot;vowels and&quot;, consonants, &quot;consonants&quot;);
            
            }
        }

<!-- end snippet -->

This still does not count uppercase letters. But I'm confident
you can fix this.

答案2

得分: 0

const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];

const vowels = fruits.flatMap(i => [...i.toLowerCase()])
  .filter(c => 'aeiou'.includes(c)).length;

const consonants = fruits.join('').length - vowels;

console.log(vowels, consonants)

或者使用正则表达式,感谢Dave的评论:

const fruitsString = ["Apple", "Orange", "Pear", "Peach", "Strawberry", 
"Cherry", "Acai"].join('')

const vowels = fruitsString.match(/[aeiou]/ig).length

const consonants = fruitsString.length - vowels

console.log(vowels, consonants)
英文:

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

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

const fruits = [&quot;Apple&quot;, &quot;Orange&quot;, &quot;Pear&quot;, &quot;Peach&quot;, &quot;Strawberry&quot;, &quot;Cherry&quot;, &quot;Acai&quot;];

const vowels = fruits.flatMap(i=&gt;[...i.toLowerCase()])
  .filter(c=&gt;&#39;aeiou&#39;.includes(c)).length;

const consonants = fruits.join(&#39;&#39;).length - vowels;

console.log(vowels, consonants)

<!-- end snippet -->

or using regex, thanks to Dave's comment below:

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

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

const fruitsString = [&quot;Apple&quot;, &quot;Orange&quot;, &quot;Pear&quot;, &quot;Peach&quot;, &quot;Strawberry&quot;, 

"Cherry", "Acai"].join('')

const vowels = fruitsString.match(/[aeiou]/ig).length

const consonants = fruitsString.length - vowels

console.log(vowels, consonants)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 01:49:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487670.html
匿名

发表评论

匿名网友

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

确定