For this game the user is asked to guess a pet between a dog, cat, fish,rabbit and a duck. If the user gets the answers correct within 15 seconds they move to new round but if they don't they get a hint about the pet that the computer randomized using math.floor and math.random. I really need help with this project as I have never done it before.
#I need help adding timer and hints along with how many times the user gussed in my game.
187 messages · Page 1 of 1 (latest)
@thin solar we're not doing people's homework for them, show some effort and we can help you.
If you already have code, then paste it.
@thin solar
I have like this much but it dosn'e show hints and idk why it's not looping
I have attached my code
Oh, you poor soul.
what happened
Are you allowed to use dictionaries?
what do you mean by dictionaries
var todo_list = {
'Monday': 'Mow lawn',
'Tuesday': 'Take out trash',
'Wednesday': 'Go to store'
}
Like this.
yesss
i have seen my teacher use this before
I know how to use if and else statment but I can;t get the hints to work
also the timer along with loop
just a hint, if you feel your code writing is super repetitive, there is probably a better way.
it looks like you did put a lot of effort in, but yeah there is some shorter ways to accomplish what you're doing.
I have been cryign about this code for like 2 hours I haven't created a game before so this is a bit complex for me
var hints = {
'Dog': 'This pet barks and has paws!',
'Cat': 'This pet meows and has paws!',
'Fish': 'This pet lives in a tank and blubs and has gills!'
}
So, I'd first create a dictionary of all the hints.
You can finish it out with the rest of the animals.
do I create like two different variable first varaibel that hass all the name of pets and second with hints?
dw, when code gets really messy and long it can be hard to understand.
If i do this and when the user gets the wrong answer how can i get these hints to display
A dictionary is a mapping of strings to other strings, so here if I were do to,
console.log(hints['Dog'])
// This pet barks and has paws!
We're just storing these for now, we will use them later.
Shortening your code so it's less complex and more understanding.
Well, I sort of have the complete answer.
I am going to modify it a bit though to make sure you're learning haha.
var hints = {
'Dog': 'This pet barks and has paws!',
'Cat': 'This pet meows and has paws!',
'Fish': 'This pet lives in a tank and blubs and has gills!'
};
var animalOptions = ["Dog", "Cat", "Fish", "Rabbit", "Duck"];
var animalOption = animalOptions[Math.floor(Math.random() * animalOptions.length)];
function game() {
while(true) {
var userGuess = prompt("Guess a pet between: Dog, Cat, Fish, Rabbit, and Duck");
if(userGuess == animalOption) {
console.log("You guessed correctly!");
return;
} else {
console.log("You guessed wrong, here's a hint: " + hints[animalOption]);
}
}
}
game();
omgggg
You're going to have to add back your stuff about asking if they have pets.
Lemme know if you have any questions about the code.
//create an array of words
var animals=["dog",
"cat",
"fish",
"duck",
"rabbit"
];
//pick random word
var animals = animals[Math.floor(Math.random()* animals.length)]
//set up the array answer
var answerArray = [];
for (var i = 0; i < animals.length; i++) {
answerArray[i] = "_";
}
var remainingLetters = animals.length;
//the game loop
while (remainingLetters>0){
//show the play
I created this
do u know how to add like a timer
!formatting js
what's this?
So you can print your code nicely.
Is this a different problem?
no it's the same in the code
Well, see if you can modify the code I posted to do that.
Look at the while loop, and it's condition.
while loop o yea i see it
Create a variable that counts the number of failed tries, and if that number is bigger than 5, say like "Game over" and then return from the function.
what did i do wrong in this
that it wasn't working
without the hints
i am a bit confused
ophh
If the user gets the answers correct within 15 seconds they move to new round
hmm
yessss
I can only think about setTimeout
can u give me example it would help a lot
var hints = {
"Dog": "This pet barks and has paws!",
"Cat": "This pet meows and has paws!",
"Fish": "This pet lives in a tank and blubs and has gills!",
"Rabbit": "This pet hops and eats carrot",
"Duck" : "This pet quacks"
};
var animalOptions = ["Dog", "Cat", "Fish", "Rabbit", "Duck"];
var animalOption = animalOptions[Math.floor(Math.random() * animalOptions.length)];
function game() {
while(true) {
var userGuess = prompt("Guess a pet between: Dog, Cat, Fish, Rabbit, and Duck");
if(userGuess == animalOption) {
console.log("You guessed correctly!");
return;
} else {
console.log("You guessed wrong, here's a hint: " + hints[animalOption]);
}
}
}
game();
I finsiehd the codeee
except the time out part
I don't even know if this is possible.
is it possible to add like how many times the user has guessed the answer
or like if the user gets the answers correct they get a point
Well, let's get to your original point.
ok
yeah this actually isn't possible to do.
You can't interrupt their prompt and give them a hint.
but the code u sedn me work i can get hints
it's an assignment
for like a culminating
that's worth 20 percent
i have to submit it by tdy midnight
i don't think timer i nessceary but it does say to add like a point counter or to keep track of how many time the user guessed
They gave you this prompt?
the prompt was about numbers but the teacher said not everyone can have the same so change it up and i chsoe pets
the prompt was to guess a number
i can't even change my prompt back
I need to know if the timer was apart of the prompt...
Because it's not possible to do a timer while waiting on a prompt.
@grand rapids can confirm.
yes the timer was part of it
like the user just gets 15 seconds and if they don't guess next round
plays
i need the exact prompt.
from the person who assigned it to you
if you can give that.
This is sort of possible.
but, the hints parts isn't like what you said.
there isn't an exact prompt lemme find
how do they end the game?
-5 pets will be randomly picked
-Every pets will have unique properties
-The user will get hints if they guess wrong answer
-The user will start guessing and will have 15 seconds to guess and if they don't move to next round
- User will get a score, depending on how
many pets are guessed right
after 3 times of rounds
o also in the beginign of the game I am suppsoed to ask the user if they have pets and either way the game starts
that's what i found on the list
of requriments
var hints = {
"Dog": "This pet barks and has paws!",
"Cat": "This pet meows and has paws!",
"Fish": "This pet lives in a tank and blubs and has gills!",
"Rabbit": "This pet hops and eats carrot",
"Duck" : "This pet quacks"
};
var animalOptions = ["Dog", "Cat", "Fish", "Rabbit", "Duck"];
function guess(correctAnswer) {
var userGuess = prompt("Guess a pet between: Dog, Cat, Fish, Rabbit, and Duck");
if(userGuess == correctAnswer) {
return true;
} else {
return false;
}
}
function game() {
var rounds = 0;
while(rounds < 5) {
var correctAnswer = animalOptions[Math.floor(Math.random() * animalOptions.length)];
var startRoundTime = Date.now();
while(true) {
var response = guess(correctAnswer);
if((startRoundTime + 5 * 1000) < Date.now()) {
console.log("You ran out of time, moving to next round.");
break;
}
if(response == true) {
console.log("You guessed correctly and earned 5 points!");
break;
} else {
console.log("You guessed wrong, here's a hint: " + hints[correctAnswer]);
}
}
rounds++;
}
}
game();
I've left the point collecting for you to solve.
oooo thank you so much
Take time to learn what it does.
The guess function returns true or false depending on if they've guessed the right answer.
The way we handle the time is by recording the time at the start of the round, and then checking when they answer if they've gone over time.
does it always have to be in the beginign like the timer
okkk
i get what you did if((startRoundTime + 5 * 1000) < Date.now()) { this sentence declares the timer
and the 1000 is i belive 1 second
It's not really a timer, but yeah we use it like that.
I guess timer is a good word.
well, 1000 miliseconds is 1 second.
so 5 * 1000 is 5 seconds.
ahhh i seee
i will work and add more details if I have further question can i still messgae u
?
yes, but just please go through the code, break it, mess with it
and figure it out
it's for the best.
understand why we are using two while loops.
the first whille loop is for how many rounds there will be
like 5 means there are 5 rounds
i'd recommend trying to recreate the code above from what you've learned
and use it as a reference
retyping it and understanding each line will be good practice
you have some assurance that you're almost done with the problem
so you can spend time not stressing and learn
may i know why u said almost done
you have points to add
yea like other animals
ooooo
5 seconds is a real challenge though lol
i played it a few times
and had to really type quick lol
yeaaa i will add 15 seconds
i don't get this
we tell them they get 5 points, but we don't record it
after the game is done, you should tell them their score
should i use console.log
np