【问题标题】:Function not working功能不工作
【发布时间】:2016-01-15 16:39:56
【问题描述】:

你好,我的功能 questionAnswer 根本不起作用。假设提示关闭 switch 命令。我用这个网站寻求帮助。 http://www.w3schools.com/jsref/met_win_prompt.asp

var keyword1 = "how are"
var keyword2 =  "what are" 
var keyword3 = "creator"
var keyword4 = "why"
var keyword5 = "how did"
var keyword6 = "Where were"
var keyword7 = "what do"
var keyword8 = "color"
var keyword9 = "Bye" 
var keyword10 = "animal"
var keyword11 = "Hello"

var robotAnswer1 = "Good"
var robotAnswer2 = "I am the new Lego “Dames” droid 7.0. I am the newest and most  advanced robot in this century!"
var robotAnswer3 = "My creator is James Day, I was programmed by Emanuel “Kiwi Elefant” Rotible"
var robotAnswer4 = "I was made to inspire imagination and bring smiles!"
var robotAnswer5 = "I was created with state-of-the-art technology in a lab."
var robotAnswer6 = "In a lab at Lego’s Headquarters"
var robotAnswer7 = "I can talk, move listen, respond and can teach programming to kids"
var robotAnswer8 = "I do not know what you are trying to say" 
var robotAnswer9 = "I was made to inspire imagination and bring smiles"
var robotAnswer10 = "I was created with state-of-the-art technology in a lab"
var robotAnswer11 = "In a lab at lego's Headquarters"

function questionAnswer()  {
var text;
var questionAnswer = prompt(“Ask me a Question!”);
switch(questionAnswer) {
    case questionAnswer :
    	text = "Excellent choice! Whatever you said is good for your soul.";
        break;
    case keyword1:
    	text = robotAnswer1;
        break;
case keyword2:
    	text = robotAnswer2;
        break;
case keyword3:
    	text = robotAnswer3;
        break;
case keyword4: 
    	text = robotAnswer4;
        break;
case keyword5: 
    	text = robotAnswer5;
        break;
case keyword6:
    	text = robotAnswer6;
        break;
}
<!DOCTYPE html>
<html>
  <head>
 < script>
var keyword1 = "how are"
var keyword2 =  "what are" 
var keyword3 = "creator"
var keyword4 = "why"
var keyword5 = "how did"
var keyword6 = "Where were"
var keyword7 = "what do"
var keyword8 = "color"
var keyword9 = "Bye" 
var keyword10 = "animal"
var keyword11 = "Hello"

var robotAnswer1 = "Good"
var robotAnswer2 = "I am the new Lego “Dames” droid 7.0. I am the newest and most  advanced robot in this century!"
var robotAnswer3 = "My creator is James Day, I was programmed by Emanuel “Kiwi Elefant” Rotible"
var robotAnswer4 = "I was made to inspire imagination and bring smiles!"
var robotAnswer5 = "I was created with state-of-the-art technology in a lab."
var robotAnswer6 = "In a lab at Lego’s Headquarters"
var robotAnswer7 = "I can talk, move listen, respond and can teach programming to kids"
var robotAnswer8 = "I do not know what you are trying to say" 
var robotAnswer9 = "I was made to inspire imagination and bring smiles"
var robotAnswer10 = "I was created with state-of-the-art technology in a lab"
var robotAnswer11 = "In a lab at lego's Headquarters"

function questionAnswer()  {
var text;
var questionAnswer = prompt(“Ask me a Question!”);
switch(questionAnswer) {
    case questionAnswer :
    	text = "Excellent choice! Whatever you said is good for your soul.";
        break;
    case keyword1:
    	text = robotAnswer1;
        break;
case keyword2:
    	text = robotAnswer2;
        break;
case keyword3:
    	text = robotAnswer3;
        break;
case keyword4: 
    	text = robotAnswer4;
        break;
case keyword5: 
    	text = robotAnswer5;
        break;
case keyword6:
    	text = robotAnswer6;
        break;
}
</head>
<body>
  <title> Robot interaction </Title>
    <base target="_top">
   <img class="irc_mi" style="margin-top: 2px;" src="http://engineering.missouri.edu/wp-content/uploads/Lego-Robot.jpeg" width="304" height="390">

  </script>
</form>

</body>


<button onclick ="questionAnswer();">Ask</button>
</script>
 





<body>
<script>

</body>


</html>

【问题讨论】:

  • 你检查过控制台吗?你的代码有问题case :
  • 我想你会发现你的问题出在空壳上,...case : text = "Excelle...,你试过用devtools调试它吗?
  • 为什么在 HEAD 中有一个表单?
  • 对不起........我会试试的。
  • 而不是使用大小写,有keywords = { "how are you": "Good", "what are you":"I am the new....\"Dames\".....", ..... }所以你可以使用keywords[answer]代替

标签: javascript html


【解决方案1】:

首先,将function questionAnswer {替换为function questionAnswer () {

其次,您在function questionAnswer 末尾缺少} 闭包

第三,将所有实例替换为"'

第四,第一种情况,case : 是空的 - 把一些值放在那里。

第五,正如 mplungjan 建议的那样,将表单放入正文中。

作为一般做法,请使用调试器测试您的 html,例如,在 Chrome 中,按 F12 并刷新页面以查看是否有任何错误。

希望对你有帮助

【讨论】:

  • 第六,因为文本没有在任何地方使用,“计算”值将被简单地丢弃。暂时没有输出。
  • 好点@Thomas,让我们假设代码的逻辑要继续。
  • 不客气@Manuela Rugemintwaza,如果您觉得我的回答有用,请将其标记为已接受
猜你喜欢
  • 2014-12-13
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多