2011-06-27 47 views
0

查看此帖子的底部;無法調用函數

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); 
+2

*未捕獲的ReferenceError:未定義sString * –

回答

2

您反覆sString至極不存在,而不是你的參數aString

0

sString在你的函數中聲明的位置是什麼?用aString(或聲明var sString = aString)嘗試並重試。

0

請更改function myF(aString)function myF(sString)

0

有一個命名錯誤。這裏是你的代碼的工作副本。 http://jsfiddle.net/hXarY/

您可以嘗試使用「螢火」趕上這樣的錯誤,如果你不已經這樣做了。

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); 
0

既然您知道是什麼原因導致錯誤,您可能需要考慮縮短代碼。這是相當龐大和多餘的。以下是一個建議:

function isVowel(char) { 
    return /[aeiouy]/i.test(char); 
} 

function myF(str){ 
    var i = 0 
     ,current 
     ,prevIsVowel = false 
     ,result = [] 
    ; 
    for (; i<str.length; i++){ 
     current = isVowel(str[i]); 
     if (current && !prevIsVowel){ 
     result.push(str[i]); 
     } 
     prevIsVowel = current; 
    } 
    return result.join(''); 
} 
console.log("the cat sat on the mat"); //=> eaaoea