字符串不是函数错误?

时间:2014-09-09 10:00:50

标签: javascript string

背景

我正在使用Code Academy学习JavaScript,并坚持他们设置的任务之一。他们提供的控制台没有提供错误输入行的指导。它提供的唯一帮助是错误消息。

问题

我收到此错误消息

  

TypeError:string不是函数

来自这个JavaScript块

confirm("Are you ready to play?");
var age = prompt("what is your age?");

if(age > 13){
    console.log("You are allowed to play but we hold no responsiblity");
}else{
    console.log("You can play, crack on");
}

console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");

console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'"); 
var userAnswer = prompt("Do you want to race Bieber on stage?");

if (userAnswer === "yes"){
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}else{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

var feedback = prompt("Rate this game out of 10 bitch!");

if (feedback > 7){
    console.log("Thank you! We should race at the next concert!");
}else{

      console.log("Ill keep practicing coding and racing.");
}

任何想法来自哪里都会有很大的帮助。

修改

在进行各种用户建议的更改后,代码仍然标记为错误,错误消息仍然存在。

有关何时退出脚本的详细信息

脚本在提示符后退出:

  

"你的年龄是多少"?

3 个答案:

答案 0 :(得分:8)

错字,

 console.log="You and Bieber start racing. It's neck and neck! You win by a shoelace!";

放括号。

console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");

答案 1 :(得分:2)

这些行:

console.log = "You and Bieber start racing. It's neck and neck! You win by a shoelace!";
console.log = "Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'";

...将console.log(这是一个函数)更改为字符串。虽然这是一个错误,但它不会产生任何错误,至少不会立即产生错误。在以下行中引发错误:

console.log("Thank you! We should race at the next concert!");
console.log("Ill keep practicing coding and racing.");

您正在尝试将字符串作为函数调用,类似于:

"aaa"();

这会抛出错误“字符串不是函数”。只能使用()调用函数,函数变量,函数表达式(等)。

答案 2 :(得分:1)

试试这个:     确认(“你准备好了吗?”);     var age = prompt(“你的年龄是多少?”);

if(age > 13){
    console.log("You are allowed to play but we hold no responsiblity");
}else{
    console.log("you can play, crack on");
}

console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");

console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'"); 
var userAnswer = prompt("Do you want to race Bieber on stage?");

if (userAnswer === "yes"){
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}else{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

var feedback = prompt("Rate this game out of 10 bitch!");
if (feedback == 7){
    console.log("Thank you! We should race at the next concert!");
}
else{
  console.log("Else Thank you! We should race at the next concert!");
}