Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

That's the error I'm getting from the Google Chrome JavaScript console when I try to run my page. It refers me to the following line

function char_counter(var str)

from the following code

<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>

What's the deal??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
414 views
Welcome To Ask or Share your Answers For Others

1 Answer

Function parameters are local to the function anyway, therefore the var is disallowed there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...