未捕获的SyntaxError:意外的令牌var [英] Uncaught SyntaxError: Unexpected token var
本文介绍了未捕获的SyntaxError:意外的令牌var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试运行我的网页时,这是我从谷歌Chrome JavaScript控制台获得的错误。它引用了我的以下行:
$ p $ 函数char_counter(var str)
来自以下代码
< html>
< head>
< script type =text / javascript>
函数text_change_steps()
{
/ *第一步:
从单元格获取文本
* /
document.write(好的,我们做了它进入功能);
var textStart = document.getElementById(ipt).value;
var textTarget = document.getElementById(tgt).value;
/ *第2步:
创建2个地图,一个用于需要添加到textStart的字母,
另一个用于需要从textStart中移除的字母。
示例:
如果textStart =dude和textTarget =deck,则以下过程
创建2个映射,needAdded和needRemoved with key-value pair
needAdded ['c '] = 1,needAdded ['k'] = 1,needRemoved ['u'] = 1,
和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);
/ *第3步:
显示需要移除的内容以及需要添加的内容。演示过程。
* /
if(!is_empty(needRemoved))
{
for(var k in needRemoved)
{
document.write(< p>需要删除+ needRemoved [k] +occurence+(needRemoved [k]> 1?s:)+of< i>+ k +< / i>< ; / p>中);
}
}
}
函数char_counter(var str)
{
/ *给定字符串str,返回其键的映射是str中包含
的字符,其值是每个字符的相应计数。
举例:给定aabnsab,返回retMap,其中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;
}
返回retMap;
}
函数map_disjunt(var m1,m2)
{
/ *给定映射m1和m2具有相同的键类型和集合$ b中的值$ b {0,1,2,...}返回一个映射,其键是m1中的每个键,即
的m2,并且其值大于m2中的相应值,或者是
在m1但不是m2。价值将是差异。
例如:
如果m1有m1 ['a'] = 3和m1 ['b'] = 1且m2有m2 ['a'] = 1,则retMap ['a'] = 2
和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;
}
返回retMap;
}
函数is_empty(var M)
{
/ *用于确定地图是否为空的函数
* /
for( var k in M)
if(M.hasOwnProperty(k))
return false;
返回true;
}
< / script>
< style type =text / css>
< / style>
< / head>
< body>
< p>盯着单词:< / p>
< input type =textt id =ipt/>
< p>结束文字:< / p>
< input type =textid =tgt/>
< p>< button onclick =text_change_steps()>显示转换步骤< / button>< / p>
< / body>
< / html>
什么是交易?
解决方案
功能参数对函数来说是本地的,因此 var
不允许在那里使用。
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??
解决方案
Function parameters are local to the function anyway, therefore the var
is disallowed there.
这篇关于未捕获的SyntaxError:意外的令牌var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文