Rabbit Olympic Racing JavaScript To start this assignment, open the script.js file in your editor and read through the notes left in the comments. You will be filling in the code based on the comments...

1 answer below »

Rabbit Olympic Racing


JavaScript


To start this assignment, open thescript.jsfile in your editor and read through the notes left in the comments. You will be filling in the code based on the comments in this file.


Some things to keep in mind:


Your rabbits should be named "Rabbit 1" through "Rabbit 4"; this will make it easier when you're coding.



  • Write the internal logic of thegetRandomSpeedfunction so that it returns arandomwhole numberbetween 1 and 10(this will be used to randomly generate each rabbit's speed).Hint:Math.random()returns a float, so you'll need to figure out which function is used toconvert a float to an integer.


In the Animal class:



  • inside therunmethod, add a conditional check thatthrows an errorwith message'Cannot supply rabbit with negative speed.'if a negative speed is supplied to the method. This will ensure that your code won't make the rabbits run backwards!



Follow the rest of the notes in thescript.jsfile to fill in the contents of the file.







Note:HTML and CSS file is ready , only the javascript needs to be done.




/** * The comments through this file indicate the expected functionality * required to complete the exercise. */ /** * This is the base Animal class; all Rabbits will extend this class. */ class Animal { constructor(name) { this._speed = 0; this._name = name; } getSpeed() { return this._speed; } getName() { return this._name; } } /** * This is the Rabbit class; all new Rabbits that are created will call the * constructor of this class to instantiate a new rabbit object. */ class Rabbit extends Animal { constructor(name) { super(name); } hop(speed) { // Prevent negative numbers from interferring with speed // ... this._speed += speed; } stop() { this._speed = 0; } getRacerId() { return this._name.toLowerCase().replace(/\s+/, '-'); } } // Declare two global variables: // - one to house all rabbit instances // - one to store the timer id from setInterval function getRandomSpeed() { // Return a random whole number between 1 and 10 } function initRace() { // Enable the Stop button // Create 4 new Rabbit instances with names like 'Rabbit 1' through to 'Rabbit 4'. // Remember: the name is passed to the constructor! // Create a global array containing all of the rabbit objects you created above. // Assign the timer id that is returned from the setInterval call above with 500ms intervals to the global variable `timer` // Loop through all the rabbits // Call `.hop(getRandomSpeed())` on each rabbit // NOTE: This is what moves the rabbit to the right from the start line. // We use the `calc` function in CSS to calculate the speed across the screen in `vw` units // and subtract 10rem units (half the width of our 20rem wide rabbit images) so we get the center of the rabbit image. // Uncomment this line below when you get this far // const calculatedValue = `calc(${speed <= 6="" 6="" :="" speed}vw="" -="" 10rem)`;="" get="" the="" rabbit="" image="" by="" it's="" class="" name="" using="" `.getracerid()`="" and="" add="" a="" `left:="" ...;`="" style="" using="" the="" previous="" calculated="" value="" determine="" the="" winner="" here="" by="" filtering="" the="" rabbits="" and="" returning="" the="" ones="" with="" a="" speed="" greater="" than="" or="" equal="" to="" 98="" if="" the="" filtered="" winners="" array="" length="" is="" greater="" than="" 1="" alert="" the="" name="" of="" the="" rabbit="" that="" won="" the="" race="" disable="" the="" stop="" button="" call="" `stoprace()`="" }="" function="" stoprace()="" {="" loop="" over="" all="" the="" rabbits="" and="" call="" `.stop()`="" on="" each="" rabbit="" clear="" the="" `timer`="" interval="" }="" **="" *="" domready="" is="" a="" 3rd-party="" library="" that="" is="" loaded="" in="" the="" html="" file="" and="" used="" here.="" *="" it="" exposes="" a="" utility="" function="" that="" we="" call="" which="" takes="" a="" callback="" as="" the="" *="" first="" and="" only="" argument="" to="" it,="" which="" we="" use="" to="" call="" our="" own="" code="" to="" initialize="" the="" page.="" */="" domready(()=""> { document .querySelector('.js-btn-start-race') .addEventListener('click', () => { alert('Starting the race!'); initRace(); }); document .querySelector('.js-btn-stop-race') .addEventListener('click', () => { stopRace(); alert('Stopping the race!'); }); });
Answered Same DayMar 30, 2021

Answer To: Rabbit Olympic Racing JavaScript To start this assignment, open the script.js file in your editor...

Aditya answered on Mar 30 2021
150 Votes
Assingment/checker.png
Assingment/grass.jpg
Assingment/index.html













Start
Stop




Assingment/rabbit-1.png
Assingment/rabbit-2.png
Assingment/rabbit-3.png
Assingment/rabbit-4.png
Assingment/script.js
/**
* The comments through this file indicate the expected functionality
* required to complete the exercise.
*/
/**
* This is the base Animal class; all Rabbits will extend this class.
*/
class Animal {
constructor(name) {
this._speed = 0;
this._name = name;
}

getSpeed() {
return this._speed;
}

getName() {
return this._name;
}
}

/**
* This is the Rabbit class; all new Rabbits that are created will call the
* constructor of this class to instantiate a new rabbit object.
*/
class Rabbit extends Animal {
constructor(name) {
super(name);
}

hop(speed) {
// Prevent negative numbers from interferring with speed
// ...
if(speed < 0)
{
alert('Cannot supply rabbit with negative speed');
}
else
{
this._speed += speed;
}

}

stop() {
this._speed = 0;
}

getRacerId() {
return this._name.toLowerCase().replace(/\s+/, '-');
}
}

// Declare two global variables:
// - one to house all rabbit instances
// - one to store the timer id from setInterval
var rabbitArray = [];
var timerId;

function getRandomSpeed() {
// Return a random whole number between 1 and 10
var randomNumber = Math.floor(Math.random() * 9 + 1);
return randomNumber;
}

function initRace() {
// Enable the Stop button
let button = document.querySelector(".js-btn-stop-race");
button.disabled = false;
// Create 4 new Rabbit instances with names like 'Rabbit 1' through to 'Rabbit 4'.
// Remember: the name is passed to the constructor!
var rabbit1 = new Rabbit('Rabbit 1');
var rabbit2 = new Rabbit('Rabbit 2');
var rabbit3 = new Rabbit('Rabbit 3');
var rabbit4 = new Rabbit('Rabbit 4');
// Create a...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here