yarn install
There is a fundamentals section in the workshop. Start by completing this section before moving on to the following exercises.
See the README.md for more information.
By the end of this exercise, you will have a track, a stable of frogs that you can race, and everytime you load the page, random frogs from the stable will race a predetermined distance. Gambling is not condoned or encouraged.
All of the work will be done inside of __workshop/exercise-1/
folder. Take a few minutes to see what's in there. _You will write the majority of your code in app.js
.
- Write a
for
loop that will generate a "lane" for each frog. Each lane should be anli
and inside eachli
there should be a span for the lane number. There is a variable calledFROGS
inexercise-1/app.js
. Use that to determine how many times to run thefor
loop. - Each lane should be an
li
. - Inside each
li
there should be aspan
that contains the lane number.
We are going to need to target each lane separately at some point in our program. So let's give each lane its own id
as well.
- Assign each lane an
id
following the this pattern:lane-<LANE_NUMBER>
. e.g.lane-1
,lane-2
,lane-3
. - If you preview the page, you should now see an ordered list, with the lane number as its only value.
Here is some pseudo-code to get you started. ๐
// Exercise 1.1 - Set up the track
for (let count = 1; count <= FROGS; count++) {
// create a lane (<li>)
// give lane an id
// create a <span>
// add the lane number to the span
// append the span to the lane
// append the lane to the track
}
We need to add our stable of frogs to the app. You will find a file called frogstable.js
in exercise-1/assets/
.
- Link that file in your
index.html
file. It should be added right before theapp.js
file is added. This will allow the JS inside ofapp.js
to have access to the contents offrogstable.js
<script src="./assets/frogstable.js"></script>
- Define an empty array called
racers
. Push random frogs from the stable into this array. - Write a
for
loop that keeps pushing frogs fromfrogstable
intoracers
until we have the required number of frogs (as defined byFROGS
). - Output the racers array to the console to make sure you actually have frogs in there.
// Exercise 1.2 - Call in the frogs!
// declare a variable called racers and assign a value of: empty array
// write a for loop
// push a frog from frogstable into racers array
// console.log racers
We need to assign each frog to a lane.
- Use the racers array to assign each frog to a lane.
- Append the frog's name and racing number in the lane.
- Set the background color of the frog element to the color assigned to it in its object.
HINT: Use a forEach
loop and make use of the optional callback argument id
. More info on W3 Schools.
- Link the provided stylesheet (
/assets/styles.css
) in ourhtml
file.
<link rel="stylesheet" href="./assets/styles.css" />
Revisit the loop you wrote in 1.3 and
- add a class of
frog
to each of the frogs so that we can make use of the provided styling. - Move the frog name to its own
span
, and give that span a class offrog-name
.
We will soon need to track each frog's progress as they hop down the track. Let's use the racers
array to do that.
Revisit the loop from 1.3 and
- Set each frog's progress to 0 inside its object, inside of the racers array.
- Set the frog's lane inside its object as well. (We'll need this later.)
- Verify the console.log of
racers
to validate.
- Create a function called
racingFrog
. It will accept one parameter. This parameter will be a frog object from the racers array. Put a console.log in there for testing purposes.
function racingFrog(racer) {
console.log("racingFrog() ", racer);
}
- Write a
forEach
that will callracingFrog()
for each frog in theracers
array.
Let's get back to our racingFrog() function, and add some logic that will do the folowing every second:
- calculate a random hop distance
// Calculate the hop distance (as a percentage of the `trackWidth`) with the following formula.
const trackWidth = track.offsetWidth;
const hopLength = Math.floor(((Math.random() * 100) / trackWidth) * 100);
- Declare a variable called
hop
and assign it asetInterval
. This interval will the hopLength toracer.progress
every second. - Move the
hopLength
to the function inside thesetInterval
- add that distance to the frog's progress
- Console for verification.
const hop = setInterval(function () {
const hopLength = Math.floor(((Math.random() * 100) / trackWidth) * 100);
// add hopLength to progress
// add a console log to verify
}, 1000);
Notice that this will continue forever!! ๐ฌ
- Add logic to the
setInterval
callback to stop the frog hopping beyond a progress of100
. - Once a frog reaches
100
, useclearInterval
to stop the process. - Write a message to the console, once the frog has reached the end.
๐ก - Minimally complete workshop (75%) - ๐ก
Examine the CSS file and determine what property you can modify to get them moving on the screen. Once you know which CSS property to modify, modify it inside of the setInterval
callback instead of the console.log
that announces the bounce distance.
If you have ever seen a frog race, it isn't all that exciting. The frogs are generally not that motivated to reach the end... To that end, let's give each frog its own random delay time for the setInterval to simulate a frog waiting around between hops.
๐ข - Complete workshop (100%) - ๐ข
- Create an array named
ranking
; - As soon as a frog reaches the end, push it into the array.
- Once all of the frogs have finished, write a message to the console declaring the first, second and third place frogs (as applicable).
Hint: You will need to use setInterval
...
It would be great if we could pick frogs at random from the stable, instead of simply taking them in order. See what you can change to make this happen. You will most likely need to use a while
loop instead of a for
loop.
The race needs some sprucing up. Make the game a more presentable! Flex those CSS muscles.
This could also include
- replace the beautiful circles with actual frog images.
- announcing the winner in the page.
- printing each frog's progress.
- a podium for the winners.
- etc.