【问题标题】:Syntax Error: Unexpected Token语法错误:意外的令牌
【发布时间】:2015-03-31 16:37:35
【问题描述】:

这里是新手程序员!

我在 codeacademy.org 上学习并不断收到此错误,即使我很确定它已正确编写。帮忙?

// Declare a variable on line 3 called
// myCountry and give it a string value.
var myCountry = "Panama";
// Use console.log to print out the length of the variable myCountry.
console.log(.length myCountry);

// Use console.log to print out the first three letters of myCountry.
console.log(myCountry .subscript(0,3));

【问题讨论】:

  • 这是什么语言?

标签: javascript syntax-error token


【解决方案1】:

你的例子有两个问题:

console.log(.length myCountry)

不是有效的Javascript;你需要

console.log(myCountry.length)

length 是变量myCountry 的属性)。

另外,subscript 不是 JS 函数,你需要substring

完整示例:

// Declare a variable on line 3 called
// myCountry and give it a string value.
var myCountry = "Panama";
// Use console.log to print out the length of the variable myCountry.
console.log(myCountry.length);

// Use console.log to print out the first three letters of myCountry.
console.log(myCountry.substring(0, 3));

【讨论】:

  • 非常感谢您指出这一点。我现在可以继续教程了!您抽出时间来帮助他人学习真是太好了,我希望有一天能学到足够的知识并以同样的方式帮助他人。
【解决方案2】:

这是正确的答案

var myCountry = "myCountry";
console.log("myCountry".length);


var myCountry = "myCountry";
console.log("myCountry".substring(0,3) );

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2019-01-17
    • 1970-01-01
    • 2017-10-07
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多