简体   繁体   English

Javascript SyntaxError:意外令牌

[英]Javascript SyntaxError: Unexpected token

I'm learning Javascript on Codecademy, and one of the tasks is to use a .push() method to find my name in a string containing many names, with the letters seperated by colons. 我正在Codecademy上学习Javascript,其中一项任务是使用.push()方法在包含许多名称的字符串中找到我的名字,并用冒号分隔字母。 I don't need it to log to the console just yet, though. 不过,我现在还不需要它登录到控制台。 It keeps telling me that I have the following Syntax Error: 它不断告诉我,我有以下语法错误:

SyntaxError: Unexpected token .

Here is my current code: 这是我当前的代码:

/*jshint multistr:true */
var text = "John Steve Ray John John Steve Connor John Bob Frank Ray Connor John Bob John Bob Jim Connor Bob John Ray Frank";
var myName = "Connor";
var hits = [];
for (var i=0; i<text.length; i++) {
};
if (text[i]==="C") {
    for (var j=i; j<i+myName.length; j++) {
        var hits.push(j);
    };
};

All contributions will be greatly appreciated, thanks :) 所有的贡献将不胜感激,谢谢:)

var text = "John Steve Ray John John Steve Connor John Bob Frank Ray Connor John Bob John Bob Jim Connor Bob John Ray Frank";
var myName = "Connor";
var hits = [];
for (var i=0; i<text.length; i++) {
if (text[i]==="C") {
    for (var j=i; j<i+myName.length; j++) {
        hits.push(j);
    };
}
}

Remove the var before hits.push(). 在hits.push()之前删除var。

Make sure you to close the for loop after if 确保在if之后关闭for循环

var text = "John Steve Ray John John Steve Connor John Bob Frank Ray Connor John Bob John Bob Jim Connor Bob John Ray Frank";
var myName = "Connor";
var hits = [];
for (var i=0; i<text.length; i++) {
if (text[i]==="C") {
    for (var j=i; j<i+myName.length; j++) {
        hits.push(j);
    }
}
};

You are declaring hits again and you had a forloop for (var i=0; i<text.length; i++) { empty so make sure you are using it right 您再次声明了hits ,并且有一个forloop for (var i=0; i<text.length; i++) {空,请确保使用正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM