Reputation: 374
I am an extremely new (read as three hours old) amateur with JavaScript so I have an extremely low level question. However I thought it would offer a great chance to explore this stackoverflow community. I have run through approx 50% of the CodeAcademy JavaScript intro and just finished the section on while and for loops. On the free practice section I decided to try and write a program to simulate a coin flip 1,000 times and report the results to the user. The program seems to run OK but on line 9 I am seeing a hint that syntax is wrong as I introduce an if/else statement. Also I now see that if you answer "no" it runs anyways. What is wrong with the syntax and what other general feedback do you have on my very first independent program? Thanks!
var userReady = prompt("Are you ready for me to flip a coin one thousand times?");
var flipCount = 0;
var heads = 0;
var tails = 0;
if (userReady = "yes" || "Yes") {
flipCount++;
while (flipCount <= 1000) {
var coinFace = Math.floor(Math.random() * 2);
if (coinFace === 0) {
heads++;
flipCount++;
} else {
tails++;
flipCount++;
}
}
} else {
confirm("Ok we'll try again in a second.");
var userReady = prompt("Are you ready now?");
}
confirm("num of heads" + " " + heads);
confirm("num of tails" + " " + tails);
var userReady = prompt("Are you ready for me to flip a coin one thousand times?");
var flipCount = 0;
var heads = 0;
var tails = 0;
if (userReady = "yes" || "Yes") {
flipCount++;
while (flipCount <= 1000) {
var coinFace = Math.floor(Math.random() * 2);
if (coinFace === 0) {
heads++;
flipCount++;
} else {
tails++;
flipCount++;
}
}
} else {
confirm("Ok we'll try again in a second.");
var userReady = prompt("Are you ready now?");
}
confirm("num of heads" + " " + heads);
confirm("num of tails" + " " + tails);
Upvotes: 1
Views: 58
Reputation: 166
I made a few changes to the code: 1. Added a default answer of yes 2. Changed the while loop to a for loop (in your code you can put the flipCount directly after the else statement) 3. Changed the confirm to alert 4. Made the number of heads and tails in one alert
var userReady = prompt("Would you like to run a coin simulator?" + "\n"
+ "Answer yes or no:","Yes");
if(userReady.toLowerCase() === "yes")
{
var heads = 0, tails = 0;
for (var flipCount = 0; flipCount < 1000; flipCount++)
{
var coinFace = Math.floor(Math.random() * 2);
if (coinFace === 0)
{
heads++;
}
else
{
tails++;
}
}
alert("Number of heads: " + heads + "\n"
+ "Number of tails: " + tails);
}
else
{
// statement goes here if the user doesn't want to play
//alert("You're no fun!");
}
Upvotes: 1
Reputation: 6010
Your if statement only looks for boolean statements (of which "Yes" is not one). Also, =
is the assignment operator, whereas ==
is the comparison operator. Changing your if statement line to the following will solve the problem.
if (userReady == "yes" || userReady == "Yes") {
Upvotes: 1
Reputation: 11
When you compare the values you should use == instead of =, also for or operation its not correct, you need to do something like : if (userReady == "yes" || userReady == "Yes")
Upvotes: 0
Reputation: 993303
This line:
if (userReady = "yes" || "Yes") {
does not do what you expect it to. First, you cannot use =
to compare values (it means assignment in Javascript). So you can use ===
. Second, the ||
joins two independent conditions, not values. So you can write:
if (userReady === "yes" || userReady === "Yes") {
Additionally, you can cover the case where the user types something like YES
or yEs
by normalising the case of the user input before comparison:
if (userReady.toLowerCase() === "yes") {
Upvotes: 3