repl.it
@cmclau01/

vowelCount function with loop

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* 

Vowel Count

Fullstack Academy - JavaScript JumpStart

- Description:

Create the function "vowelCount(str)" that takes a str parameter and returns the number of vowels 
the string contains (ie. "Fullstack Academy" would return 5).  

**Do not count 'y' as a vowel for this challenge.**

**Example**

- INPUT: `vowelCount("Good Job");`
- Return Value: `3`

*** Write the vowelCount Function below *** */
/* Original code which passed tests:
function vowelCount (str) {
  let vowelCount = 0;
  let sanitizedString = str.toLowerCase();
  for (let i = 0; i < sanitizedString.length; i++) {
    if (sanitizedString.charAt(i) === 'a' || sanitizedString.charAt(i) === 'e' || sanitizedString.charAt(i) === 'i' || sanitizedString.charAt(i) === 'o' || sanitizedString.charAt(i) === 'u') {
      console.log(str.charAt(i));
      vowelCount++;
    }
  }
  return vowelCount;
}*/
//Condensed solution below
function vowelCount (str) {
  let vowelCounter = 0;
  let vowels = 'aeiou';
  for (let i = 0; i < str.length; i++) {
    if (vowels.indexOf(str[i].toLowerCase()) > -1) {
      console.log(str[i]);
      vowelCounter++;
    }
  }
  return vowelCounter;
}  
vowelCount('Fullstack Academy');
/* calling indexOf() on vowels and passing it the value of str[i].toLowerCase accomplishes three things: first, it condenses the if statement by eliminitating the need to use the || operator to check the index of each individual vowel, and instead will check if the current string character is any of the characters in the vowels string (e.g. vowels.indexOf() > -1, which indexOf will return if a passed value is not present); second, it checks whether the index of the function parameter (str[i]) is true under this condition and will increment the vowelCounter variable if so; third, calling .toLowerCase() on str[i] within the indexOf method enables characters to be evaluated by the conditional statement regardless of case. */









































































/* Test Cases */ 

// console.log(vowelCount('Good Job'));
// logs => 3

// console.log(vowelCount(Yankees));
// logs => 3


Native Browser JavaScript
?