James James - 5 months ago 6
Javascript Question

Javascript: Issue with if/for loop

I'm trying to get my function to return certain responses depending on how many vowels the input has. However, it seems that it copies the entire word into the new array.

Expected: die Jürgen, instead got: der Jürgen


Can anyone tell me where I'm messing up?

function derDieDas(wort){
var vowels = ['a', 'e', 'i', 'o', 'u', 'ä', 'ö', 'ü'];
var newArray = [];
for(var i in wort) {
for(var j in vowels) {
if(i === j) {
newArray[newArray.length] = i;
};
};
};
if(newArray.length < 2) {
return 'das ' + wort;
} else if(newArray.length < 4) {
return 'die ' + wort;
} else {
return 'der ' + wort;
}
};

Answer

You are comparing indices

if (i === j) {

instead of elements of the arrays like

if (wort[i] === vowels[j]) {