Skip to content

Instantly share code, notes, and snippets.

@PatrickLandin
Created September 14, 2014 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatrickLandin/2b5dfd6a449cea88757c to your computer and use it in GitHub Desktop.
Save PatrickLandin/2b5dfd6a449cea88757c to your computer and use it in GitHub Desktop.
Formula 1 race where user gets to pick weather conditions. First one to 3,050 meters.
<script>
function Driver(name, speed, skill, reliability) {
this.name = name;
this.speed = speed;
this.skill = skill;
this.reliability = reliability;
this.position = 0;
this.isFocused = function() {
return Math.floor(Math.random() * 10) < this.skill;
};
this.advance = function() {
if (this.isFocused()) {
this.position += this.speed;
}
};
this.progressReport = function() {
return this.name + " is at: " + this.position;
};
this.trackCondition = function() {
this.weatherToday = prompt("For today's F1 race, are the track conditions hot, mild, or wet?");
switch(this.weatherToday) {
case 'hot':
console.log("Ooooooh, it's a hot one baby!");
break;
case 'mild':
console.log("Looks like a typical day in London, har har!");
break;
case 'wet':
console.log("Uh oh, it's a rainy one. It's got the many damps on the track place.");
break;
}
};
if (this.weatherToday == 'hot') {
return this.speed -1;
}
else if (this.weatherToday== 'mild') {
return this.speed;
}
else if (this.weatherToday == 'wet') {
return this.speed -3;
}
else {
console.log("You're bad at typing. Pick a track condition please.");
}
}
var hamilton = new Driver ("Lewis Hamilton", 8, 9, 6);
var rosberg = new Driver ("Nico Rosberg", 8, 7, 8);
var alonso = new Driver ("Fernando Alonso", 6, 9, 6);
var vettel = new Driver ("Sebastian Vettel", 6, 9, 8);
var meters = 3050;
while (hamilton.position < meters && rosberg.position < meters && alonso.position < meters && vettel.position < meters) {
hamilton.advance();
rosberg.advance();
alonso.advance();
vettel.advance();
console.log(hamilton.progressReport() + " | " + rosberg.progressReport() + " | " + alonso.progressReport() + " | " + vettel.progressReport());
}
for (i=0; i <=1; i++) {
hamilton.speed = hamilton.trackCondition();
rosberg.speed = rosberg.trackCondition();
alonso.speed = alonso.trackCondition();
vettel.speed = vettel.trackCondition();
}
if (hamilton.position >= meters) {
alert("Hammy wins!");
}
else if (rosberg.position >= meters) {
alert("Rosberg wins. Booooo.");
}
else if (alonso.position >= meters) {
alert("Alonso wins. Wow! That never happens");
}
else if (vettel.position >= meters) {
alert("Seb wins! Is it 2011 again???");
}
else {}
</script>
@ZBryan
Copy link

ZBryan commented Sep 17, 2014

I am still getting track conditions each time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment