-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (53 loc) · 1.92 KB
/
index.js
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
function Car(drivername, make, model, year, color){
this.drivername = drivername;
this.make = make,
this.model = model,
this.year = year,
this.color = color,
this.drive = function() {
document.getElementById("whodrives").textContent = `${this.drivername} drives ${this.color} ${this.model}.`;
};
}
let index = 0;
let cars = [];
function create(){
const drivername = document.getElementById("drivername").value;
const make = document.getElementById("make").value;
const model = document.getElementById("model").value;
const year = document.getElementById("year").value;
const color = document.getElementById("color").value;
cars.push(new Car(drivername,make,model,year,color));
document.getElementById("carlists").style.display = 'inline-block';
show();
}
function show(){
let latestcar = cars[cars.length -1];
document.getElementById("results").textContent = `Make: ${latestcar.make}\nModel: ${latestcar.model}\nYear: ${latestcar.year}\nColor: ${latestcar.color}\n`;
latestcar.drive();
document.getElementById("results").style.whiteSpace = "pre-line";
index = cars.length-1;
}
function previous(){
if(index==0){
index = cars.length-1;
}
else{
index-=1;
}
let latestcar = cars[index];
document.getElementById("results").textContent = `Make: ${latestcar.make}\nModel: ${latestcar.model}\nYear: ${latestcar.year}\nColor: ${latestcar.color}\n`;
latestcar.drive();
document.getElementById("results").style.whiteSpace = "pre-line";
}
function next(){
if(index>=cars.length-1){
index = 0;
}
else{
index+=1;
}
let latestcar = cars[index];
document.getElementById("results").textContent = `Make: ${latestcar.make}\nModel: ${latestcar.model}\nYear: ${latestcar.year}\nColor: ${latestcar.color}\n`;
latestcar.drive();
document.getElementById("results").style.whiteSpace = "pre-line";
}