JavaScript 'TypeError: string is not a function'

标签 javascript

这是在 applab 上运行的,我的代码第 55 行出现错误。它说 TypeError: string is not a function。我似乎无法弄清楚是否是它所在的函数或上面代码中的某个地方运行不正常,如果有人可以看一下并找出问题所在,我将不胜感激。

onEvent("startButton", "click", function() { 
  setScreen("safeScreen");
  endGame();
});//changes to the main games screen,after 2 minutes ends game
onEvent("retryButton", "click", function() { 
  setScreen("welcomeScreen");
});// changes screen to welcome screen from win screen
onEvent("againButto", "click", function() {
  setScreen("welcomeScreen");
});//changes screen to welcome screen from lose screen

function endGame(){
  setTimeout(function(){
    setScreen("loseScreen");
  },120000);
}
var input1=0;
setText("comboDisplay1",input1);//sets display1 to 0
var input2=0;
setText("comboDisplay2",input2);//sets display2 to 0
var input3=0;
setText("comboDisplay3",input3);//sets display3 to 0
onEvent("upButton1", "click", function(){
  input1 = input1 + 1;
  setText("comboDisplay1",input1);
});//when up button1 is pressed the number display changes
onEvent("upButton2", "click", function(){
  input2= input2 + 1;
  setText("comboDisplay2",input2);
});//when up button2 is pressed the number display changes
onEvent("upButton3", "click", function(){
  input3= input3 + 1;
  setText("comboDisplay3",input3);
});//when up button3 is pressed the display changes
onEvent("downButton1", "click", function(){
  input1 = input1 - 1;
  setText("comboDisplay1",input1);
});//when down button1 is pressed the number display changes
onEvent("downButton2", "click", function(){
  input2 = input2 - 1;
  setText("comboDisplay2",input2);
});//when down button2 is pressed the number display changes
onEvent("downButton3", "click", function(){
  input3 = input3 - 1;
  setText("comboDisplay3",input3);
});//when down button3 is pressed the number display changes

var playerInput;
var combination;
combination = [randomNumber(0,9),randomNumber(0,9),randomNumber(0,9)];//generates a random list of 3 numbers from 0 to 9
console.log(combination);

playerInput += getText("comboDisplay1","comboDisplay2","comboDisplay3");//assings the display numbers to the variable playerInput
function yellowLight() {
  if (getNumber("comboDisplay1") != combination(0)) {
    if (getNumber("comboDisplay1") == (combination(1)||combination(2))) {
      showElement("yellowLight1");
    }
  }
  if (getNumber("comboDisplay2") != combination(1)) {
    if (getNumber("comboDisplay2") == (combination(0) || combination(2))) {
      showElement("yellowLIght2");
    }
  }
  if (getNumber("comboDisplay3") != combination(3)) {
    if (getNumber("comboDisplay3") == (combination(0) || combination(1))) {
      showElement("yellowLight3");
    }
  }
}
function greenLight(){
  if(getNumber("comboDisplay1") == combination(0));
    showElement("greenLight1");
  if(getNumber("comboDisplay2") == combination(1));
    showElement("greenLight2");
  if(getNumber("comboDisplay3") == combination(2));
    showElement("greenLight3");
}
//checks and shows a green light if the number is correct and in the right place
onEvent("submitButto", "click", function(){
  if( playerInput== combination){
    greenLight();
  }
    yellowLight();
});

最佳答案

组合的类型是数组。您必须使用 [] 访问其元素,而不是 (),例如组合[0]

关于JavaScript 'TypeError: string is not a function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42977928/

相关文章:

javascript - react - 类型错误 : Cannot read property 'renderSidebaritem' of undefined

javascript - 如何更改 winston 日志格式?

javascript - 字符串大小写更改

javascript - ajax客户端到服务器端

javascript - X 个字符后自动提交表单

javascript - Rails 4 形式 : conditional display of fields based on radio button selection

Javascript 帮助通过 URL 传递数据

javascript - Redux State 使用 Reselect 导出相关数据

javascript - 结合数据/变量 jquery

java - 带有特殊字符的速度模板变量名 - 一个点