1

I an working on a project for an Introductory Programming class so I'm using basic javascript. This is our first project with functions and for some reason I can't seem to make it work. I called all my variables and created the function before the program starts but for some reason it skips over running the function in my program. Any help would be appreciated.

This is just the beginning of my program, I don't wanna write the rest of the code until I figure out why this part is broken, thats why the program doesn't do anything but close the window if it doesnt pass the tests.

// 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.");
}
Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96

5 Answers5

7

You have;

if(numTrees == 5, 10)​

The erroneous comma is causing the if to evaluate the truthy expression 10 so its always passing the test, to test for 5, 6, 7, 8, 9 or 10;

if(numTrees >= 5 && numTrees <= 10)
Hinek
  • 9,519
  • 12
  • 52
  • 74
Alex K.
  • 171,639
  • 30
  • 264
  • 288
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)​
Community
  • 1
  • 1
jbabey
  • 45,965
  • 12
  • 71
  • 94
1

if(numTrees == 5, 10)​ doesn not mean If numtrees is equal to 5,6,7,8,9, or 10

change it to

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

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
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)

Chris
  • 4,661
  • 1
  • 23
  • 25
0

What you want is something more like this:

    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";
    }
Anthony Mills
  • 8,676
  • 4
  • 32
  • 51