#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
#

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.

#

It will be great if someone can write me a code by today

short vale
#

@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.

tame summitBOT
#

@thin solar

Rehanthebeast Uploaded Some Code
Uploaded these files to a Gist
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

short vale
#

Oh, you poor soul.

thin solar
#

what happened

short vale
#

Are you allowed to use dictionaries?

thin solar
#

what do you mean by dictionaries

short vale
#
var todo_list = {
    'Monday': 'Mow lawn',
    'Tuesday': 'Take out trash',
    'Wednesday': 'Go to store'
}
#

Like this.

thin solar
#

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

short vale
#

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.

thin solar
#

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

short vale
#
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.

thin solar
#

do I create like two different variable first varaibel that hass all the name of pets and second with hints?

short vale
thin solar
short vale
short vale
thin solar
#

okkk

#

i will open my console

#

give me a sec

short vale
#

Shortening your code so it's less complex and more understanding.

thin solar
#

okk

#

done

#

i got the dictionary

#

and I understood them

short vale
#

Great, give me a moment.

#

To code up a better example.

thin solar
#

okk

#

sure.

short vale
#

Well, I sort of have the complete answer.

thin solar
#

seriosuly

#

that's the best

short vale
#

I am going to modify it a bit though to make sure you're learning haha.

thin solar
#

okkk

#

i don't mind

short vale
#
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();
thin solar
#

omgggg

short vale
#

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.

thin solar
#

//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

short vale
#

!formatting js

tame summitBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```js
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

thin solar
short vale
#

So you can print your code nicely.

thin solar
#

ooooo i see

#

do u think the code u sent

#

will loop

short vale
#

Is this a different problem?

thin solar
#

no it's the same in the code

short vale
#

What do you mean by timer?

#

like how many tries the user has?

thin solar
#

like the user gets a limited time to guess the answer

#

or the pet

#

yess

short vale
#

Well, see if you can modify the code I posted to do that.

#

Look at the while loop, and it's condition.

thin solar
#

while loop o yea i see it

short vale
#

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.

thin solar
#

that it wasn't working

#

without the hints

short vale
#

ophh

#

If the user gets the answers correct within 15 seconds they move to new round

#

hmm

thin solar
#

yessss

short vale
#

I can only think about setTimeout

thin solar
#

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

short vale
#

hmm

#

I misread the prompt.

#

We don't need a loop here.

thin solar
#

oooo i c i worked that out

#

i have the code done just need the timer

#

to work

short vale
#

I don't even know if this is possible.

thin solar
#

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

short vale
#

Well, let's get to your original point.

thin solar
#

ok

short vale
#

yeah this actually isn't possible to do.

#

You can't interrupt their prompt and give them a hint.

thin solar
#

but the code u sedn me work i can get hints

short vale
#

did you come up with this project

#

or is this a homework assignment

thin solar
#

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

thin solar
#

the prompt was to guess a number

#

i can't even change my prompt back

short vale
#

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.

thin solar
#

yes the timer was part of it

#

like the user just gets 15 seconds and if they don't guess next round

#

plays

short vale
#

i need the exact prompt.

#

from the person who assigned it to you

#

if you can give that.

short vale
thin solar
#

yes

#

that's what the promot

#

says

short vale
#

but, the hints parts isn't like what you said.

thin solar
#

there isn't an exact prompt lemme find

short vale
#

how do they end the game?

thin solar
#

-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
thin solar
#

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

short vale
#
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.

thin solar
#

oooo thank you so much

short vale
#

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.

thin solar
#

does it always have to be in the beginign like the timer

short vale
#

What do you mean?

#

Take time and read the code.

thin solar
#

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

short vale
#

It's not really a timer, but yeah we use it like that.

thin solar
#

what does 5

#

mean

short vale
#

I guess timer is a good word.

#

well, 1000 miliseconds is 1 second.

#

so 5 * 1000 is 5 seconds.

thin solar
#

ahhh i seee

#

i will work and add more details if I have further question can i still messgae u

#

?

short vale
#

yes, but just please go through the code, break it, mess with it

#

and figure it out

#

it's for the best.

thin solar
#

yess i get that thank so much for the help

#

i willl play arodun with it

#

now

short vale
#

understand why we are using two while loops.

thin solar
#

the first whille loop is for how many rounds there will be

#

like 5 means there are 5 rounds

short vale
#

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

thin solar
#

yess ofc

#

suree

short vale
#

you have some assurance that you're almost done with the problem

#

so you can spend time not stressing and learn

thin solar
#

may i know why u said almost done

short vale
#

you have points to add

thin solar
#

yea like other animals

short vale
#

i mean, recording points.

#

so the person knows their score

thin solar
#

ooooo

short vale
#

5 seconds is a real challenge though lol

#

i played it a few times

#

and had to really type quick lol

thin solar
#

yeaaa i will add 15 seconds

thin solar
short vale
#

we tell them they get 5 points, but we don't record it

#

after the game is done, you should tell them their score

thin solar
#

should i use console.log

short vale
#

You're going to be able to answer that. 🙂

#

I'm going to head to bed now.

thin solar
#

okkkk

#

thanks for all the help

short vale
#

np