LoLo LoLo - 5 months ago 20
jQuery Question

Print only vowels from string from textbox

I want to use two for loops and use

charAt
so if I put in "Banana" in the text box it will only show "aaa"


#textbox
= textbox

#lbl
= label (where the result will show)


This is what I have, and it's not working correctly...

$("#vow").click(function () {
var vowels = "aeiou";
var v = $("#textbox").val();
var label = $("#lbl").html(v);

for (i = 0; i < vowels.length; i++) {
vowels.charAt(i) == v.charAt(j);
for (j = 0; j < v.length; j++) {
label = v;
}
}
});

Answer

"Simple" code (at least to read):

var vowels = "aeiou";
var label = "banana";

var stripped = "";
for (i = 0; i < label.length; i++) {
  if(vowels.indexOf(label[i]) >= 0) stripped += label[i];
}
console.log(stripped);

But I advice you to simply use :

"banana".replace(/[^aeiouy]/ig, "")

I is a regex that says: "replace everything that is not one of aeiouy to nothing". i and g are flags: i = case insensitive, g = global (replace all occurrences)

Comments