未捕获的SyntaxError:意外的令牌变量

时间:2014-03-25 23:25:33

标签: javascript html

当我尝试运行我的网页时,这是我从谷歌Chrome JavaScript控制台获得的错误。它引用了我的以下一行

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 个答案:

答案 0 :(得分:1)

函数参数是函数的本地函数,因此不允许使用var