将数组元素与字符进行比较

时间:2014-02-05 23:15:26

标签: javascript html

我试着写一个简单的javascript程序来检查一个字母是否是一个元音。问题是输出不正确,应该说" a是元音。"

使用Javascript:

function findvowel(letter1, vowels) {
    var count = vowels.length;
    for (var i = 0; i < count; i++) {
        if (vowels[i] === letter1) {
            var message1 = " is a vowel";
            document.getElementById('exercise3').innerHTML = letter1 + message1;
        } else {
            var message2 = " is a consonant";
            document.getElementById('exercise3').innerHTML = letter1 + message2;
        }
    }
}

HTML:

<script>
    $(document).ready(function() {
        findvowel("a",["a","e","i","o","u"]);
    });
</script>

输出:

a is a consonant

5 个答案:

答案 0 :(得分:2)

break添加到您的循环中,以便它不会继续。

function findvowel(letter1, vowels) {
    var count = vowels.length;
    for (var i = 0; i < count; i++) {
        if (vowels[i] === letter1) {
            var message1 = " is a vowel";
            document.getElementById('exercise3').innerHTML = letter1 + message1;
            break;
        } else {
            var message2 = " is a consonant";
            document.getElementById('exercise3').innerHTML = letter1 + message2;
        }
    }
}

当元音匹配时,你实际上可以使用return false;立即停止你的功能,但是在正常情况下会使用break,因为循环后可能会有其他代码。

顺便说一句:

function findvowel(letter){
    //thanks p.s.w.g for reminding me []
    return letter+" is a "+(/[aeiou]/i.test(letter)?"vowel":"constant");
}

答案 1 :(得分:2)

您正在for循环中测试元音,并且每次都更新输出。因此,仅当测试的 last 元音与输入匹配时,才会显示输出。相反,如果找到元音,你应该突破for - 循环,并且在你测试了所有元音之后只显示失败(" is a consonant")并且你找不到匹配:

var count = vowels.length;
for (var i = 0; i < count; i++) {
    if (vowels[i] === letter1) {
        var message1 = " is a vowel";
        document.getElementById('exercise3').innerHTML = letter1 + message1;
        return;
    }
}

var message2 = " is a consonant";
document.getElementById('exercise3').innerHTML = letter1 + message2;

但是这种方法可以简化为:

function findvowel(letter1) {
    var isVowel = "aeiou".indexOf(letter1) > -1;
    var message = letter1 + " is a " + (isVowel ? "vowel" : "consonant");
    document.getElementById('exercise3').innerHTML = message;
}

答案 2 :(得分:1)

这就是我要做的,使用原生函数:

var letter = "a";
var isVowel = ["a","e","i","o","u"].some(function(vowel){
     return vowel === letter;
});

转发你的信息,我会尝试类似的事情:

var message =  letter + (isVowel ? " is a vowel" : " is a consonant");

答案 3 :(得分:0)

由于你已经在使用提供$ .inArray()的jQuery,为什么不这样做呢?

var vowels = ["a", "e", "i", "o", "u"];

$(document).ready(function() {

    var letter = 'u';

    var found = $.inArray(letter, vowels) > -1;

    if(found) {
        console.log(letter + ' is a vowel');
    } else {
        console.log(letter + ' is a consonant');
    }

});

答案 4 :(得分:0)

我会传入一个对象而不是一个数组,并利用使用'in'关键字的常量时间查找。不需要循环。

function findvowel(letter1, vowels) {
        if (letter1 in vowels) {
            var message1 = " is a vowel";
            document.getElementById('exercise3').innerHTML = letter1 + message1;
        } else {
            var message2 = " is a consonant";
            document.getElementById('exercise3').innerHTML = letter1 + message2;
        }
}

然后

var obj = {'a': true, 'e': true, 'i': true, 'o': true, 'u': true}

然后调用它

findvowel('a', obj)