2014-03-25 75 views
-2

这是我在尝试运行我的页面时从Google Chrome JavaScript控制台中获得的错误。它是指我下面一行未捕获的语法错误:意外的令牌var

function char_counter(var str) 

从下面的代码

<html> 

<head> 
<script type="text/javascript"> 
    function text_change_steps() 
    { 
     /* STEP 1: 
      Get text from cells 
     */ 
     document.write("Okay, we made it into the function."); 
     var textStart = document.getElementById("ipt").value; 
     var textTarget = document.getElementById("tgt").value; 
     /* STEP 2: 
      Create 2 maps, one for the letters that need added to textStart, 
      the other for the letters that need removed from textStart. 
      EXAMPLE: 
      If textStart="dude" and textTarget="deck", then following procedure 
      creates 2 maps, needAdded and needRemoved with key-value pairs 
      needAdded['c']=1, needAdded['k']=1, needRemoved['u']=1, 
      and needRemoved['d']=1. 
     */ 
     var startChars = char_counter(textStart); 
     var targetChars = char_counter(textTarget); 
     var needAdded = map_disjunct(startChars, targetChars); 
     var needRemoved = map_disjunct(targetChars, startChars); 
     /* STEP 3: 
      Display what needs removed and what needs added. Demonstrate process. 
     */ 
     if (!is_empty(needRemoved)) 
     { 
      for (var k in needRemoved) 
      { 
       document.write("<p>Need to remove " + needRemoved[k] + " occurence" + (needRemoved[k] > 1 ? "s" : "") + " of <i>" + k + "</i></p>"); 
      } 
     } 

    } 
    function char_counter(var str) 
    { 
    /* Given a string str, return a map whose keys are the characters contained 
     in str and whose values are the corresponding count of each character. 
     EXAMPLE: Given "aabnsab", return retMap where retMap['a']=3, retMap['b']=2, 
       retMap['n']=1, retMap['s']=1. 
    */ 
     var retMap = {}; 
     for (var k = 0, n = str.length; k < n; ++k) 
     { 
      if (str[k] in retMap) 
       ++retMap[str[k]]; 
      else 
       retMap[str[k]] = 1; 
     } 
     return retMap; 
    } 

    function map_disjunt(var m1, m2) 
    { 
    /* Given maps m1 and m2 with the same key type and with values in the set 
     {0, 1, 2, ...}, return a map whose keys are every key from m1 that is 
     in m2 and has a value greater than the corresponding one in m2, or is 
     in m1 but not m2. The value will be the difference.  
     EXAMPLE: 
     If m1 has m1['a']=3 and m1['b']=1 and m2 has m2['a']=1, then retMap['a']=2 
     and retMap['b']=1. 
    */ 
     var retMap = {}; 
     for (var k in m1) 
     { 
      var otherCount = (k in m2) ? m2[k] : 0; 
      if (m1[k] > otherCount) 
       retMap[k] = m1[k] - otherCount; 
     } 
     return retMap; 
    } 

    function is_empty(var M) 
    { 
    /* Function to determine whether a map is empty 
    */ 
     for (var k in M) 
      if (M.hasOwnProperty(k)) 
      return false; 
     return true; 
    } 
</script> 
<style type="text/css"> 

</style> 
</head> 

<body> 
    <p>Staring word:</p> 
    <input type="text"t id="ipt"/> 
    <p>Ending text:</p> 
    <input type="text" id="tgt"/> 
    <p><button onclick="text_change_steps()">Show transformation steps</button></p> 
</body> 

</html> 

这是怎么回事?

+1

用'函数char_counter(str)'替换'函数char_counter(var str)',你不是声明一个变量,而是引用一个参数 –

+1

没有必要在函数声明中的参数前放入'var'在javascript中,所以你也应该从你的其他函数中移除'var'。 – Erik

+0

错误告诉你这个问题,并引导你到达确切的路线。那么你在这里需要更多的信息? –

回答

1

函数参数对函数来说是本地的,因此var不允许在那里。

相关问题