JavaScript Syntax Error or More? -
i extremely new (read 3 hours old) amateur javascript have extremely low level question. thought offer great chance explore stackoverflow community. have run through approx 50% of codeacademy javascript intro , finished section on while , loops. on free practice section decided try , write program simulate coin flip 1,000 times , report results user. program seems run ok on line 9 seeing hint syntax wrong introduce if/else statement. see if answer "no" runs anyways. wrong syntax , other general feedback have on first independent program? thanks!
var userready = prompt("are ready me flip coin 1 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 second."); var userready = prompt("are ready now?"); } confirm("num of heads" + " " + heads); confirm("num of tails" + " " + tails); var userready = prompt("are ready me flip coin 1 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 second."); var userready = prompt("are ready now?"); } confirm("num of heads" + " " + heads); confirm("num of tails" + " " + tails);
this line:
if (userready = "yes" || "yes") { does not expect to. first, cannot use = compare values (it means assignment in javascript). can use ===. second, || joins 2 independent conditions, not values. can write:
if (userready === "yes" || userready === "yes") { additionally, can cover case user types yes or yes normalising case of user input before comparison:
if (userready.tolowercase() === "yes") {
Comments
Post a Comment