2012-08-01 73 views
1

我正在为一个介绍性编程类的项目工作,所以我使用基本的JavaScript。这是我们的第一个功能项目,由于某种原因,我似乎无法使其工作。我在程序启动之前调用了所有的变量并创建了函数,但由于某种原因它跳过了我的程序中的函数运行。任何帮助,将不胜感激。Javascript跳过功能

这只是我的程序的开始,我不想编写代码的其余部分,直到我找出为什么这个部分被破坏,这就是为什么程序没有做任何事情,但如果它没有关闭窗口通过测试。

// 1 Declare Variables 
var numTrees; 
var counter = 0; 
var answer = "no"; 

function treeFunction(answer, counter, numTrees) { 
    while (answer == "no" && counter < 3) { 
     if (numTrees == 5, 10) { 
      answer = "yes"; 
     } else if (numTrees < 5 || numTrees > 10) { 
      alert("That is an incorrect value.\nThe sample size should be less than 5 or greater than 10.\nPlease try again."); 
      answer = "no"; 
      numTrees = prompt("Please reenter the amount of trees in your sample."); 
      counter + 1; 
     } 
    } 
    if (answer == "no") { 
     alert("You have entered an incorrect number too many times.\nThe Program will now end."); 
     window.open('', '_self', ''); 
     window.close(); 
    } else if (answer == "yes") { 
     return; 
    } 
} 
// 2 Prompt the Instructor for the number of Trees 
numTrees = prompt("How many trees are in your sample?"); 
alert("You have entered: " + numTrees); 
treeFunction(answer, counter, numTrees) 
document.write(numTrees); { 
    document.write("<br/> <br/>" + "End of Program."); 
} 
+0

我没有运行的代码,但我也注意到你失踪后'treeFunction分号(答案,counter,numTrees)' – kei 2012-08-01 15:13:20

+6

'if(numTrees == 5,10)'是什么意思? – 2012-08-01 15:14:02

+1

@kei:不会引起任何问题 – 2012-08-01 15:14:28

回答

0

你需要的是更多的东西是这样的:

if (numTrees < 5 || numTrees > 10) { 
     alert("That is an incorrect value.\nThe sample size should be less than 5 or greater than 10.\nPlease try again."); 
     answer = "no"; 
     numTrees = prompt("Please reenter the amount of trees in your sample."); 
     counter + 1; 
    } else { 
     answer = "yes"; 
    } 
+0

我没有想到这一点。这是完美的! – 2012-08-01 15:44:17

7

你有;

if(numTrees == 5, 10)​ 

的错误逗号导致该if评估truthy表达10所以它总是通过测试,以测试5,6,7,8,9或10;

if(numTrees >= 5 && numTrees <= 10) 
+0

对不起...当我做出更改时,它只是完全停止运行。现在它只显示标题。 – 2012-08-01 15:41:51

1

if(numTrees == 5, 10)​没有按不意味着If numtrees is equal to 5,6,7,8,9, or 10

改变它

if(numTrees >= 5 || numTrees <=10)​

+0

这很奇怪,因为它与我用于上次作业的代码相同。 – 2012-08-01 15:22:45

+0

它可能看起来有效。 – 2012-08-01 15:26:34

+0

我想这可以解释为什么我的一些同学有这样的构建工作,有些则没有。 – 2012-08-01 15:28:45

2

您正在使用的方式这一行中的逗号有一个特别的意思荷兰国际集团:

if(numTrees == 5, 10)​ 

本质上讲这确实是返回转换为布尔值,这是不0时的10值(第二个操作数),所以它是真实的。

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comma_Operator

你可能想用的OR(||):

if(numTrees == 5 || numTrees == 10)​ 

或检查numTrees针对范围:

if(numTrees >= 5 || numTrees <= 10)​ 

在一个侧面说明,在javascript建议您总是使用identity comparison===)而不是常规比较(==):

if(numTrees === 5 || numTrees === 10)​ 
1

如果(numTrees == 5,10){ 答案= “是”; }

这是一个奇怪的构造,我从来没有见过。我假设你相信它的意思是“范围在5到10之间的numTrees?”,但事实并非如此。没有检查,我认为这意味着你一次检查两件事:

  • 是numTrees等于5?
  • 是10? (这实际上意味着“是10不是0”,这当然总是如此)。

由于您检查的第二个条件始终为真,因此您始终将答案设置为“是”。结果,你的循环总是运行一次 - 它启动,检查答案是“否”,将答案设置为“是”,并立即停止循环。

你需要改变你的条件,如果(numTrees> = 5个& & numTrees < = 10)