The year is 1950 during the Cold War. The Soviet communists are making plans to invade Antarctica. The penguins of the south aren't going down without a fight.
newkeyword to instantiate the parties)
- Each party has a name ("Penguins" and "Communists" - or whatever you want)
- Each party has a starting population of 1,000,000
- Flip a coin (random number between 1 and 2) to see which party attacks first
- This will just be a randomly generated number (Hint: Math.random(); Also this will happen only one time and at the beginning of your game)
- Create a function called
sendNuke(party, onHit, onMiss)where:
- the first parameter
- the second parameter
onHitis a callback function that will be called if the attack is successful
- the third parameter
onMissis a callback function that will be called if the attack is unsuccessful
onMissfunctions each take one parameter of type
party(just forwarding the party you already passed into sendNuke)
- Print messages in the callbacks that are appropriate - e.g. if the attack missed then it would log "The Penguins' nuke missed the Communists and instead landed in the ocean" or something along those lines
- A random algorithm will be used to determine if the attack is a miss or a hit (this can happen in the
- If an attack is successful initiate a random damage amount
- After an attack is completed (or missed) the opposing party will initiate an attack.
- This process will repeat until one of the parties reaches a 0 population
This will assess your understanding of the following topics:
- Objects/Associative Arrays
- Constructors or "Classes"
- Callback functions
- Functional programming concepts
Passing off this exercise completes Programming Principles Level 3 in the Super Transcript.
For a better user experience, add some delay between attacks using setInterval. You won't be able to do this inside of a loop, so you'll need to learn about
clearInterval as well.