1

私は入門プログラミングクラスのプロジェクトに取り組んでいるので、基本的な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.");
}
4

5 に答える 5

7

あなたが持っている;

if(numTrees == 5, 10)​

誤ったコンマにより、ifは真の式を評価する10ため、常にテストに合格し、5、6、7、8、9、または10をテストします。

if(numTrees >= 5 && numTrees <= 10)
于 2012-08-01T15:15:34.640 に答える
2

The way you are using the comma in this line has a special meaning:

if(numTrees == 5, 10)​

Essentially what this does is returns the value of 10 (the second operand) when cast to a boolean, which is not 0, so it is true.

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

You probably meant to use OR (||):

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

Or check numTrees against a range:

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

On a side note, in javascript it is recommended that you always use identity comparison (===) instead of regular comparison (==):

if(numTrees === 5 || numTrees === 10)​
于 2012-08-01T15:19:07.320 に答える
1
于 2012-08-01T15:18:54.800 に答える
1

if (numTrees == 5, 10) { answer = "yes"; }

This is an odd-looking construct that I've never seen before. I'm assuming you believe it means "is numTrees within the range 5 to 10?", but that's not the case. Without checking, I think it essentially means you're checking two things at once:

  • is numTrees equal to 5?
  • is 10? (this essentially means "is 10 not 0", which of course is always true).

Since the 2nd condition you're checking is always true, you're always setting answer to "yes". As a result, your loop always runs exactly once - it starts up, checks answer is "no", sets answer to "yes", and that immediately stops the loop.

You need to change your condition to if(numTrees >= 5 && numTrees <= 10)

于 2012-08-01T15:21:32.493 に答える
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";
    }
于 2012-08-01T15:18:45.667 に答える