This exercise allows you to create a virtual pet using JavaScript! We can program the pet to do many things such as go for walks, eat, sleep, and play. We can also code the pet to adopt a child if we want :)
- Create a fork of this repo.
- Clone your copy:
git clone git@github.com:*your-github-username*/virtual-pet.git cdinto the repository and you will see all the files here
- Running
npm installornpm ifor short will tell node package manager (npm) to download thedependenciesanddevDependenciesfor this project, outlined in thepackage.jsonfile - Once you have run
npm iyou should seenode_modules/in the repository
- Open up the
pet.test.jsfile and runnpm tin the terminal. The tests outlined here test the code written in thepet.jsfile - The
requirestatement at the top of the test file imports the functions from ourpet.jsfile into the test file. This ensures that the tests can test the functions written in thepet.jsfile - The
itblocks in the test file make assertions about what should happen when we call the function to be tested. For example:
describe('isAlive', () => {
it('tells us if the pet is no longer alive', () => {
const pet = new Pet('Fido');
pet.fitness = 0;
pet.hunger = 10;
pet.age = 30;
expect(pet.isAlive).toEqual(false);
});
});
```
- `describe('isAlive', () => { ... });` this test is testing the `isAlive` method
- `it('tells us if the pet is no longer alive', () => { ... });` tells us what the return value should be when passed a certain argument
- `expect(pet.isAlive).toEqual(false);` this calls the function pet.isAlive and expects it to return false-
cdinto thepet.test.jsfile -
Type
nodeinto the terminal - you should see:Welcome to Node.js v12.18.4. Type ".help" for more information.
- Type
const Pet = require('../src/pet');and hit enter- You can also do this from the
virtual-petfolder itself. Once you opennodetypeconst Pet = require('./src/pet');- the double..in the first example means that you need to come back out of thepet.test.jsfile and then move into thepet.jsfile (/src/pet). If you're already inside thevirtual-petfolder you're telling the computer it just needs to go inside thepet.jsfolder instead with the./src/pet
- You can also do this from the
- Copy and paste any
constlines from the individual tests. For example:const pet = new Pet('Fido');const parent = new Pet('Dave');const child = new Pet('Amelia');Copy and paste these one at a time and hitenterorreturnafter each one. These are the variables the code is working with
- Type
petinto node and hitenterorreturn- this will give you the starting values of your pet- Your pets' name should be: Fido
- Your pets' initial age should be: 0
- Yuur pets' initial hunger should be: 0
- Your pets' initial fitness should be 10
- Type
pet.growUp()to change these values- Every time you call this function on your pet their age will increase by 1, their hunger will increase by 5, and their fitness will decrease by 3
- To feed your pet run
pet.feed()- This will decrease the pets' hunger by a value of 3, to a minimum value of 0
- To exercise your pet run
pet.walk()- This will increase their fitness level by a value of 4, to a maximum value of 10
- To check on the status of your pet run
pet.checkUp()- This will allow the pet to tell you if it's hungry, needs a walk, both, or if it's A-okay
- To adopt a child for your pet run
parent.adoptChild(child)- This will create a child for your pet called 'Amelia'
- Be warned! Your pet will perish if you do not feed it or exercise it enough
- If your pets' hunger level reaches 10 or higher it will die
- If your pets' fitness level drops to 0 or lower it will die
- If your pet reaches the age of 30 or higher it will die
- Over to you now - have fun! :)
- It would be fun to add some sort of frontend development into this project. Either adding pictures or animation as a visual tool for how your pet is doing
- Node.js
- Jest - for testing
Thank you Manchester Codes for all your help creating this project! This was a fun one and was great for getting a better understanding of writing tests, writing code for those tests, and working with node REPL