2012-08-30 23 views
1

错误发生在if语句内部的第38行和第39行之间(根据Chrome JS控制台,我假设这意味着我的错误在第39行)。我不知道我做错了什么,如果任何人都可以帮助我,那真是太棒了。谢谢。如果你想看到的代码之前,我把它弄坏了,这个程序是在http://piratena.me/我有一个JavaScript“意外标识符”错误。谁能帮我?

/*This object is a simple dictionary that matches every letter of the alphabet to a pirate-related string.*/ 

var altDictionary = { 
    a: 'arr', 
    b: 'pegleg', 
    c: 'timber', 
    d: 'monkey', 
    e: 'knife', 
    f: 'powder', 
    g: 'grog', 
    h: 'scuttle', 
    i: 'keel', 
    j: 'cannon', 
    k: 'sparrow', 
    l: 'cutlass', 
    m: 'mast', 
    n: 'plank', 
    o: 'matey', 
    p: 'bag', 
    q: 'doubloon', 
    r: 'rope', 
    s: 'rum', 
    t: 'chip', 
    u: 'lubber', 
    v: 'spit', 
    w: 'patch', 
    x: 'salt', 
    y: 'tack', 
    z: 'tortuga' 
} 

/*This function loops through the input from the user, replacing each letter in their name with a pirate-related string.*/ 

var altName = function(name) { 
    var result = ""; 
    for (i=0; i<name.length; i++) { 
     var ind=name[i]; 
     if isAlpha(ind) === true { 
      ind = toLowerCase(ind); 
      var syl = altDictionary[ind]; 
      result = result + syl + "-"; 
     } else { 
      alert("Your name can only contain alphabetical letters."); 
     } 
    } 
    result = result.substring(0, result.length - 1) 
    return result; 
}; 

/*This block of jQuery inserts the newly created Pirate Name into the output field*/ 

$(document).ready(function() { 
    $('form #click_button').click(function(event) { 
     event.preventDefault(); 
     var name = $('#input_box').val(); 
     $('#output p').text(altName(name)); 
    }); 
}); 

回答

6

现场运行我注意到的第一件事情是这样的:

if isAlpha(ind) === true 

为什么没有括号?

if (isAlpha(ind) === true) 

顺便提一下,它不能帮助建议我们去你的活生生的例子,找出你有不同的代码比你在你的问题提供什么运行。

+0

嗯,这是一个很好的捕获。我来自Python,因为我不必为if语句使用parens,所以我错过了那个。我添加了parens,现在它告诉我:“未捕获的ReferenceError:isAlpha未定义”。我认为Alpha是JS中的一个全局函数。没有? –

+1

没有'isAlpha' javascript'函数'。 – Gabe

+0

恩,当然。我必须使用正则表达式来检查非A-Z字符吗? –