0
我是打字稿和網頁設計領域的全新人物,所以爲了打開門,我正在編寫一個程序,在加載和重新加載時,它會輸出一個隨機車。一個例子輸出將是以下幾點:門在打字稿中定義打印的類型
編號:缸4數:4類型的傳輸:車身的FWD類型:SUV
有你能得到其他的結果,但這只是佈局的一個例子。
我在我的打字稿文件中的代碼如下:
class Car {
car: string;
door: number;
cylinder: number;
transmission: string;
body: string;
element: HTMLElement;
constructor(door: number, cylinder: number, transmission: string, body: string) {
this.car = door + " " + cylinder + " " + transmission + " " + body;
}
}
interface FullCar {
door: number;
cylinder: number;
transmission: string;
body: string;
}
function randomCarGenerator(car: FullCar) {
return "Number of Doors: " + car.door + "Number of Cylinders: " + car.cylinder + "Type of Transmission: " + car.transmission + "Type of Car Body: " + car.body;
}
var randomBody = ['Coupe', 'Sedan', 'Wagon', 'SUV'];
var randomDoor1 = [2, 4, 5];
var randomDoor2 = [2, 4];
var randomCylinder = [2, 3, 4];
var randomTransmission1 = ['AWD', 'Standard', 'FWD'];
var randomTransmission2 = ['Standard', 'FWD'];
var body = randomBody[Math.floor(Math.random() * randomBody.length)];
if (body == 'Coupe') {
var door = 2;
var cylinder = randomCylinder[Math.floor(Math.random() * randomCylinder.length)];
var transmission = randomTransmission2[Math.floor(Math.random() * randomTransmission2.length)];
var car = new Car(door, cylinder, transmission, body);
document.body.innerHTML = randomCarGenerator(car);
}
else if (body == 'Sedan' || body == 'Wagon') {
var door = randomDoor1[Math.floor(Math.random() * randomDoor1.length)];
var cylinder = randomCylinder[Math.floor(Math.random() * randomCylinder.length)];
var transmission = randomTransmission1[Math.floor(Math.random() * randomTransmission1.length)];
var car = new Car(door, cylinder, transmission, body);
document.body.innerHTML = randomCarGenerator(car);
}
else {
var door = randomDoor2[Math.floor(Math.random() * randomDoor2.length)];
var cylinder = randomCylinder[Math.floor(Math.random() * randomCylinder.length)];
var transmission = randomTransmission1[Math.floor(Math.random() * randomTransmission1.length)];
var car = new Car(door, cylinder, transmission, body);
document.body.innerHTML = randomCarGenerator(car);
}
當我運行它,我得到的結果是門
編號:未定義缸未定義的數字等等等等
我的問題:我是如何聲明聲明值的問題?還是有一個完全不同的問題,我沒有看到?
這非常非常謝謝你! – user2580