-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (44 loc) · 1.5 KB
/
script.js
File metadata and controls
67 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// script.js
$("#go").click(function(){
//creating a function to check which car has won
function checkIfComplete() {
if(isComplete == false){
isComplete = true;
}else{
place = "second" ;
}
}
// taking carWidth
var carWidth = $("#car1").width();
// getting width of Car track
var raceTrackWidth = $(window).width() - carWidth;
// assingining random numbers to cars
var raceTime1 = Math.floor((Math.random() * 5000) + 1);
var raceTime2 = Math.floor((Math.random() * 5000) + 1);
//making variable for completing the race and keeping it false
var isComplete = false;
//making variable place and keeping default first
var place = "first";
// making a function to move the car1
$("#car1").animate({
left:raceTrackWidth
}, raceTime1 , function(){
// animate function for car is done
checkIfComplete();
// adding some text
$("#raceInfo1 span").text("Finished at" + place +"and clocked in at time" + raceTime2 + "milliseconds")
});
$("#car2").animate({
left:raceTrackWidth
},raceTime2, function(){
// animate function for car is done
checkIfComplete();
// adding some text
$("#raceInfo2 span").text("Finished at" + place +"and clocked in at time" + raceTime2 + "milliseconds")
});
//reset the cars
$("#reset").click(function(){
$(".car").css("left", "0");
$(".raceInfo").text("");
})
});