无法调用功能

时间:2011-06-27 11:02:45

标签: javascript

见本文的底部;

function isVowel(aCharacter)
    {
        return ((aCharacter == 'a') || (aCharacter == 'A')||
                (aCharacter == 'e') || (aCharacter == 'E')||
                (aCharacter == 'i') || (aCharacter == 'I')||
                (aCharacter == 'o') || (aCharacter == 'O')||
                (aCharacter == 'u') || (aCharacter == 'U')||
                (aCharacter == 'y') || (aCharacter == 'Y'));
    }


function myF(aString)
{
    // variable to hold resultString
    var resultString = '';

    // variable to hold the current and previous characters
    var currentCharacter = '';
    var precedingCharacter = '';

    // in the case of the first character in the string there is no
    // previous character, so we assign an empty string '' to the variable at first
    //precedingCharacter = '';

    // TODO  part (ii)
    // add code as directed in the question

     var i = 0;
    for( i; i < sString.length; ++i)
    {

        currentCharacter = sString.charAt(i);
        if (isVowel(currentCharacter) && (!isVowel(precedingCharacter)))
        {
        resultString = resultString + 'ub';
        }
        resultString = resultString + currentCharacter;
        precedingCharacter = currentCharacter;
    } 

    return resultString;
}

var string1 = "the cat sat on the mat"; 
var result1 = myF(string1);
document.write(string1);//THIS ISN'T GOING TO BE DISPLAYED, BUT WHY?

document.write('<BR>');
document.write(result1);

4 个答案:

答案 0 :(得分:2)

您对sString进行迭代,但不存在参数aString

答案 1 :(得分:0)

在您的函数中声明sString的位置?尝试使用aString(或声明var sString = aString)然后重试。

答案 2 :(得分:0)

请将function myF(aString)更改为function myF(sString)

答案 3 :(得分:0)

有一个命名错误。这是您的代码的工作副本。 http://jsfiddle.net/hXarY/

如果您还没有,可以尝试使用“ firebug ”来捕获此类错误。

function isVowel(aCharacter)
    {
        return ((aCharacter == 'a') || (aCharacter == 'A')||
                (aCharacter == 'e') || (aCharacter == 'E')||
                (aCharacter == 'i') || (aCharacter == 'I')||
                (aCharacter == 'o') || (aCharacter == 'O')||
                (aCharacter == 'u') || (aCharacter == 'U')||
                (aCharacter == 'y') || (aCharacter == 'Y'));
    }


function myF(sString)  // this should be sString , not aString
{
    // variable to hold resultString
    var resultString = '';

    // variable to hold the current and previous characters
    var currentCharacter = '';
    var precedingCharacter = '';

    // in the case of the first character in the string there is no
    // previous character, so we assign an empty string '' to the variable at first
    //precedingCharacter = '';

    // TODO  part (ii)
    // add code as directed in the question

     var i = 0;
    for( i; i < sString.length; ++i)
    {

        currentCharacter = sString.charAt(i);
        if (isVowel(currentCharacter) && (!isVowel(precedingCharacter)))
        {
        resultString = resultString + 'ub';
        }
        resultString = resultString + currentCharacter;
        precedingCharacter = currentCharacter;
    } 

    return resultString;
}

var string1 = "the cat sat on the mat"; 
var result1 = myF(string1);
document.write(string1);//THIS ISN'T GOING TO BE DISPLAYED, BUT WHY?

document.write('<BR>');
document.write(result1);