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.");
}
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.");
}
Share
Improve this question
edited Sep 27, 2012 at 11:50
Jason Orendorff
45.1k6 gold badges66 silver badges101 bronze badges
asked Aug 1, 2012 at 15:07
Justin McIntoshJustin McIntosh
155 bronze badges
5
-
I didn't run the code, but I did notice you're missing a semicolon after
treeFunction(answer, counter, numTrees)
– kei Commented Aug 1, 2012 at 15:13 -
6
What do you mean by
if (numTrees == 5, 10)
?? – Clyde Lobo Commented Aug 1, 2012 at 15:14 - 1 @kei : that would not cause any issues – Clyde Lobo Commented Aug 1, 2012 at 15:14
-
1
if (numTrees == 5, 10)
is effectively exactly the same asif (10)
, meaning that theelse
part will never happen. – Pointy Commented Aug 1, 2012 at 15:15 - If numtrees is equal to 5,6,7,8,9, or 10. – Justin McIntosh Commented Aug 1, 2012 at 15:16
5 Answers
Reset to default 7You have;
if(numTrees == 5, 10)
The erroneous ma 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)
The way you are using the ma 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/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 remended that you always use identity parison (===
) instead of regular parison (==
):
if(numTrees === 5 || numTrees === 10)
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)
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)
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";
}