This code isn't working: Help! It is only displaying as "no answers left", not anything else and that is displaying that I have only 0 attempts left. I am new to Javascript and need some support, I think the "for" loop isn't working. I can only try out this code once, then the page crashes.
function mathCheck() {
var inputValues1 = document.mathGuesser.mGuesser.value;
var attempts;
for (attempts = 3; attempts >= 0; ) {
if (inputValues1 === 'y = 2x - 3' || inputValues1 === 'y=2x-3') {
document.getElementById("answer").style.color = "Green";
document.getElementById("answer").innerHTML = "<span>" + "Correct" + "</span>";
}
else if (inputValues1 === ' ' || inputValues1 === '') {
document.getElementById("answer").style.color = "Black";
document.getElementById("answer").innerHTML = "<span>" + "Please enter text" + "</span>";
}
else {
document.getElementById("answer").style.color = "Red";
document.getElementById("answer").innerHTML = "<span>" + "Incorrect" + "</span>";
document.getElementById("attempts").innerHTML = attempts;
attempts--;
}
}
document.getElementById("answer").style.color = "Black";
document.getElementById("answer").innerHTML = "No attempts left";
}
In general the algorithm you came up with is working, e.g.:
for (attempts = 3; attempts >= 0; ) {
const answer = prompt("Answer to live?");
if(answer === "42"){
alert("correct!");
} else {
attempts--;
alert(attempts + " attempts left");
}
}
alert("you lost :(");
But that scenario does work because weve got synchronous code. Waiting for the user to enter the right answer is not synchronous but rather asynchronous. So in your case it will check the answer three times before the user is even to able to enter the right answer and in case the right answer was entered the loop runs forever so fast, that it freezes the browser. To solve this you need a state (remaining) and handle the different cases on every click:
var attenpts = 3;
function mathCheck() {
if(attempts <= 0)
return answer("Your tries are up!", "red");
var inputValues1 = document.mathGuesser.mGuesser.value;
if (inputValues1 === 'y = 2x - 3' || inputValues1 === 'y=2x-3') {
answer("Correct", "green");
} else if (inputValues1 === ' ' || inputValues1 === '') {
answer("Please enter your answer!", "black");
} else {
answer("Incorrect!", "red");
document.getElementById("attempts").innerHTML = attempts;
attempts--;
}
}
function answer(text, color){
const el = document.getElementById("answer");
el.innerHTML = "<span>" + text + "</span>";
el.style.color = color;
}
I don't know what you're trying to do, you should specify what the function aims to do in your question, and provide details of the issue, however this is the format of a For loop:
for(var foo = 0; foo < 10; foo++)
So your for loop should look like so:
for (attempts = 3; attempts >= 0; attempts++)
This makes "attempts" increment by exactly one on each iteration of the loop.
Note that the last part is up to you, since your variable starts at 3, and loops while greater than or equal to 0, I'm going to assume you want to decrement, so you could use: attemps--, attempts-=1..
Related
I am attempting to make the number of tries go up by one for each incorrect guess. That number would then be shown in the alert. For some reason no matter how many attempts I do it always says one. Any tips?
function checkGuess(guess, target) {
let correct = false;
let numTries = 0;
do {
if (!COLORS_ARRAY.includes(guess)) {
numTries ++;
alert('You must select one of the colors listed. Press ok to retry.');
} else if (guess < target) {
numTries ++;
alert('Incorrect. You are on try ' + numTries + '.' + '\n\nAlphabetically to high');
} else if (guess > target) {
numTries ++;
alert('Incorrect. You are on try ' + numTries + '.' + '\n\nAlphabetically to low');
} else {
correct = true;
document.body.style.background = guess;
}
return correct;
} while (!correct);
}
numTries is a variable local to the function checkGuess, so every time you execute the function it sets numTries to 0. What you can do is create the variable outside of the checkGuess function.
let numTries = 0;
function checkGuess(guess, target) {
let correct = false;
if (!COLORS_ARRAY.includes(guess)) {
numTries ++;
alert('You must select one of the colors listed. Press ok to retry.');
} else if (guess < target) {
numTries ++;
alert('Incorrect. You are on try ' + numTries + '.' + '\n\nAlphabetically to high');
} else if (guess > target) {
numTries ++;
alert('Incorrect. You are on try ' + numTries + '.' + '\n\nAlphabetically to low');
} else {
correct = true;
document.body.style.background = guess;
}
return correct;
I don't think you need to use a do-while loop here. You could do something like the following. I'm choosing the number of tries here as 3.
function checkGuess(guess, target) {
let tries = 3;
while (tries > 0) {
alert("Guess the colour - you have " + tries + " tries left.");
if (guess === target) {
return true;
} else {
tries--;
}
}
}
This code will continuously prompt the user to guess the colour using a loop that runs until the user either guesses the correct colour or runs out of tries. This may not satisfy your requirements thoroughly, but you can extract the logic from it.
I'm a new programmer learning HTML/CSS/Javascript, and was fiddling around with it until I came across a bug. I was making the computer guess my number (0-5), but then realized if I put in a number higher than 5, the website just crashes. Is there any way I can make it so that if the user puts in a number higher than 5 it will just delete it automatically? Or is that not Javascript. Thanks in advance :)
document.getElementById("guess").onclick=function() {
var gotit=false;
var guesses=1;
var x;
while(gotit==false) {
x=Math.random();
x=6*x;
x=Math.floor(x);
if(document.getElementById("myNumber").value==x) {
gotit=true;
} else {
guesses++;
}
}
alert("Got it! It was a " + x + ". It only took me " + guesses + " guesses!");
}
Try this :
document.getElementById("guess").onclick=function() {
if(document.getElementById("myNumber").value > 5) {
document.getElementById("myNumber").value = "";
alert("Please provide a number that is with in 0 to 5");
} else {
var gotit=false;
var guesses=1;
var x;
while(gotit==false) {
x=Math.random();
x=6*x;
x=Math.floor(x);
if(document.getElementById("myNumber").value==x) {
gotit=true;
} else {
guesses++;
}
}
alert("Got it! It was a " + x + ". It only took me " + guesses + " guesses!");
}
Than define a onchange function to check length:
document.getElementById("myNumber").onchange = function (ev){
try{
var target = document.getElementById("myNumber");
if(parseInt(target.value) > x) { // can throw exception, when given a non number
target.value="";
}
} catch(ex) {
alert('Not a number');
}
};
IMPORTANT:
You have a greater problem here: You are generating a random number, and then comparing it to input( that does not change). This is an infinite loop. Because this is a random operation, and you can hit same number more than once.
You need to generate your number first (before click event on 'guess' button), before clicking on quess button. Like so:
var luckyNumber = x;
var guesses=1;
document.getElementById("start").onclick=function(){ //init counters once
guesses=0;
x=Math.floor(Math.random()*6);
gotit = false;
}
document.getElementById("guess").onclick=function() { // guess as many times as you want
if(document.getElementById("myNumber").value==x) {
gotit=true;
}
guesses++;
if(gotit){
alert("Got it! It was a " + x + ". It only took me " + guesses + " guesses!");
}
}
But if you want to computer to quess your number, than you need to limit number of guesses (add a counter), or it will hang eventualy.
I took this as a little challenge and just went ahead and re-did your little game. Hope this helps.
Demo here
(function (guess, tryy, message) {
var comp = function () {
return Math.floor(Math.random() * 6)
};
var number = comp();
var count = 0;
var test = function () {
var val = guess.value;
if (!Number.isNaN(val) && val >= 0 && val <= 5) {
switch (true) {
case val > number:
message.innerHTML = 'Your guess was too high!';
count++;
break;
case val < number:
message.innerHTML = 'Your guess was too low!';
count++;
break;
case val == number:
count++;
message.innerHTML = 'Congratulations you found the number! It took you ' + count + ' guesses';
//Reseting game here
setTimeout(function(){
count = 0;
number = comp();
guess.value = '';
message.innerHTML = 'Your game has been reset';
}, 2000);
break;
};
}
};
tryy.onclick = test;
guess.onkeyup = function (e) {
if (e.keyCode == 13) {
test();
}
}
})(document.getElementById('guess'), document.getElementById('tryy'), document.getElementById('message'));
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);
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") {
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") {
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!");
}
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")
In the code below, when I set the userDoor variable to the prompted user input, the user input attaches to the variable. I know this because the value shows up in the console.log statement just below the variable declaration.
However, the variables revealDoor and offerDoor, which are set based on the userInput variable, act wonky in ways that suggest the variable didn't "catch" the value. (Wonky: easier to demonstrate than explain. please run the code below. UserDoor, revealDoor, and offerDoor should all be different numbers in the range of 1-3. Some of the time, revealDoor === userDoor. This should never happen. ).
Interestingly, the program works fine when I set userDoor directly to an integer--see commented debug line.
Why would the userInput variable show the right value in the console but not seem to catch in the rest of the program?
(This program, by the way, is meant to illustrate the monty hall paradox
alert("There are 3 doors. Behind two of them are DEATH and DOOM. Behind the other is a BIG TASTY SAUSAGE.");
var yWin = 0;
var yLoss = 0;
var nWin = 0;
var nLoss = 0;
while(true){ //noprotect
var userDoor = prompt("choose door: 1, 2, or 3");
//var userDoor = 1 //debug
console.log('UD: ' + userDoor);
var prizeDoor = randomRange(1,3);
console.log('PD: ' + prizeDoor);
var revealDoor = (function(){
var revealDoor = prizeDoor;
if (prizeDoor === userDoor) {
while (revealDoor === prizeDoor) {
revealDoor = randomRange(1,3);
}
return revealDoor;
}
else {
while (revealDoor === prizeDoor || revealDoor === userDoor){
revealDoor = randomRange(1,3);
}
return revealDoor;
}
})();
console.log('RD: ' + revealDoor);
var offerDoor = (function(){
var offerDoor = prizeDoor;
while(offerDoor === revealDoor || offerDoor === userDoor){
offerDoor = randomRange(1,3);
}
return offerDoor;
})();
console.log('OD: ' + offerDoor);
var choice = prompt("You chose door " + userDoor + ". Behind door " + revealDoor + ", I reveal DEATH AND DOOM. I would like to offer you the chance to change your choice to " + offerDoor + ". Do you accept--y/n?");
if(choice === "y"){
userDoor = offerDoor;
}
var result = (function(){
var result;
if(userDoor === prizeDoor){
alert("YOU FOUND THE PRIZE!");
result = 'w';
}
else {
alert("You chose DEATH AND DOOM!");
result = 'l';
}
return result;
})();
//Analytics:
switch(result){
case 'w':
if(choice === 'y'){
yWin++;
}
else {
nWin++;
}
break;
case 'l':
if(choice === 'y'){
yLoss++;
}
else {
nLoss++;
}
break;
}
var done = prompt("Are you Done? y/n");
if(done === "y"){
break;
}
}
function randomRange(min, max) {
return Math.floor(Math.random () * (max - min +1)) + 1;
}
var totalSamples = yWin + nWin + yLoss + nLoss;
var yChoices = yWin + yLoss;
var nChoices = nWin + nLoss;
console.log('With a sample set of ' + totalSamples);
console.log('You chose to change ' + (yChoices/nChoices * 100) + '% of the time.');
console.log('When you\'ve chosen to change, you\'ve won ' + (yWin/yChoices * 100) + '% of the time.');
console.log('When you\'ve chosen to stay, you\'ve won ' + (nWin/nChoices * 100) + '% of the time.');
var userDoor = prompt("choose door: 1,2, or 3")) * 1
Its better 2 use radio button for this
The problem is that prompt() returns a string. Which means any '===' comparisons are returning undefined. I need to convert the returned string to a number using Number().
var userDoor = Number(prompt("choose door: 1,2, or 3"))
Use
var done = confirm("Are you Done? y/n");
if (done) break;
Instead of d prompt confirmation
I am trying to make a simple JavaScript guessing game, and my for loop keeps getting skipped! Here is the part of my code that is getting skipped:
for (i = 0; i === tries; i += 1) {
isSkipped = false;
var guessedNumber = prompt("Guess your number now.");
console.log("User guessed number " + guessedNumber);
//check if number is correct
if (guessedNumber === numberToGuess) {
confirm("Hooray, you have guessed the number!");
break;
} else if (guessedNumber > numberToGuess) {
confirm("A little too high...");
} else {
confirm("A little too low...");
}
}
and here is the full code:
//declaring variables
var numberToGuess;
var tries;
var i;
var isSkipped = true;
var confirmPlay = confirm("Are you ready to play lobuo's guessing game? The number for you to guess will be a number ranging from 1 to 25."); //does the user want to play?
if (confirmPlay === true) {
console.log("User wants to play");
} else {
window.location = "http://lobuo.github.io/pages/experiments.html";
} //if user wants to play, let them play, else go to website homepage
numberToGuess = Math.floor((Math.random() * 25) + 1); //sets computer-generated number
tries = prompt("How many tries would you like?"); //gets amount of tries
tries = Math.floor(tries); //converts amount of tries to integer from string
for (i = 0; i === tries; i += 1) {
isSkipped = false;
var guessedNumber = prompt("Guess your number now.");
console.log("User guessed number " + guessedNumber);
//check if number is correct
if (guessedNumber === numberToGuess) {
confirm("Hooray, you have guessed the number!");
break;
} else if (guessedNumber > numberToGuess) {
confirm("A little too high...");
} else {
confirm("A little too low...");
}
}
if (isSkipped === true) {
console.log("Oh no! The for loop has been skipped!");
}
If you need any further details, just ask.
Shouldn't the for be like this?:
for (i = 0; i < tries; i += 1) {
When you write:
for (i = 0; i === tries; i += 0) {
the loop repeats as long as the condition i === tries is true. If tries is 3, for instance, this condition is not true on the first iteration, and the loop ends immediately.
You should write:
for (i = 0; i < tries; i++) {
Also you need to use parseInt() function on user's input.
var guessedNumber = parseInt(prompt("Guess your number now."), 10);
instead of
var guessedNumber = prompt("Guess your number now.");