admin管理员组

文章数量:1388126

Ik heb een probleem met mijn TypeScript-code. Ik probeer npm start uit te voeren, maar er gebeurt niets.

Wat ik verwacht:

  • Dat mijn script uitvoert en de console.log statements laat zien.

Wat er gebeurt:

  • npm start geeft geen errors, maar print ook niks.

Wat ik al heb geprobeerd:

  1. npm install om te controleren of alle dependencies correct geïnstalleerd zijn.
  2. npx tsc om de TypeScript-code te compileren.
  3. node dist/index.js handmatig uitvoeren, maar nog steeds geen output.

Wat doe ik fout?


Hier is mijn index:

import { Vuurwerk } from "./Vuurwerk";
import { Instructie } from "./Instructie";
import { Knaller } from "./Knaller";
import { Vuurpijl } from "./Vuurpijl";
import { Product } from "./Product";

/**
 * Controleer de veiligheid van een aantal vuurwerkpakketten
 *
 * @author Lennard Fonteijn
 */
function main(): void {
    // Stap 0: Print naam, klas en studentnummer
    console.log("naam, studentnummer, klas");

    const pakket: Vuurwerk[] = [];
    stap1(pakket);
    stap2(pakket);
    stap3(pakket);
    stap4(pakket);
    stap5(pakket);
    stap6(pakket);
}





function stap1(pakket: Vuurwerk[]): void {
    console.log("--- STAP 1 ---");

    let vuurwerk: Vuurwerk;

    vuurwerk = new Vuurwerk("Veiligheidsbril", 2.5, new Instructie(true, 6, "Draag bij aansteken"));
    pakket.push(vuurwerk);
    console.log(vuurwerk.toString());

    vuurwerk = new Vuurwerk("Safety glass", 2.5, new Instructie(false, 6, "Wear before ignition"));
    pakket.push(vuurwerk);
    console.log(vuurwerk.toString());

    vuurwerk = new Vuurwerk("Aansteeklont zonder instructie", 0.25);
    pakket.push(vuurwerk);
    console.log(vuurwerk.toString());
}





function stap2(pakket: Vuurwerk[]): void {
    console.log("--- STAP 2 ---");

    let knaller: Knaller;

    knaller = new Knaller("Celebration Crackers", 10, 777, 75, new Instructie(false, 21, "Keep minimum 10 ft distance"));
    pakket.push(knaller);
    console.log(knaller.toString());

    knaller = new Knaller("Peking Rol", 45, 500, 120, new Instructie(true, 21, "Houd minimaal 5 meter afstand"));
    pakket.push(knaller);
    console.log(knaller.toString());

    knaller = new Knaller("Shanghai Rol", 85, 1000, 125, new Instructie(true, 21, "Houd minimaal 5 meter afstand"));
    pakket.push(knaller);
    console.log(knaller.toString());

    knaller = new Knaller("Hongkong Rol", 82.50, 1000, 100);
    pakket.push(knaller);
    console.log(knaller.toString());
}







function stap3(pakket: Vuurwerk[]): void {
    console.log("--- STAP 3 ---");

    let vuurpijl: Vuurpijl;

    vuurpijl = new Vuurpijl("Cruise Rocket", 2.5, 40, [50, 25, 25], new Instructie(true, 10, "Niet in de hand houden"));
    pakket.push(vuurpijl);
    console.log(vuurpijl.toString());

    vuurpijl = new Vuurpijl("Killing Arrow", 4.25, 40, [100, 0, 0], new Instructie(true, 16, "Niet in de hand houden"));
    pakket.push(vuurpijl);
    console.log(vuurpijl.toString());

    vuurpijl = new Vuurpijl("Magic Sky", 2.75, 40, [100, 0, 0], new Instructie(false, 20, "Keep minimum 10 ft distance"));
    pakket.push(vuurpijl);
    console.log(vuurpijl.toString());

    vuurpijl = new Vuurpijl("Golden Sky", 3.25, 40, [50, 50, 0]);
    pakket.push(vuurpijl);
    console.log(vuurpijl.toString());
}

function stap4(pakket: Vuurwerk[]): void {
    toonVuurwerk(pakket);
}







function toonVuurwerk(pakket: Vuurwerk[]): void {
    console.log("--- STAP 4 ---");
    let totaalPrijs: number = 0;

    // Loop door alle vuurwerkobjecten en print ze
    for (const vuurwerk of pakket) {
        console.log(vuurwerk.toString() + "\n");
        totaalPrijs += vuurwerk._prijs; // Prijs optellen
    }

    // Print de totale kosten van het vuurwerkpakket
    console.log(`Kosten vuurwerkpakket:${Product.EURO}${totaalPrijs.toFixed(2)}`);
}








function stap5(pakket: Vuurwerk[]): void {
    console.log("--- STAP 5 ---");
    toonInstructies(pakket, -1);
    toonInstructies(pakket, 3);
    toonInstructies(pakket, 2);
    toonInstructies(pakket, -5);
}

function toonInstructies(pakket: Vuurwerk[], index: number): void {
    // Controleer of de index geldig is
    if (index < 0 || index >= pakket.length) {
        console.error("Index valt buiten grenzen.");
        return;
    }

    // Haal het vuurwerkobject op
    const vuurwerk: Vuurwerk = pakket[index];

    // Controleer of er een instructie aanwezig is
    if (vuurwerk._instructie) {
        console.log(vuurwerk._instructie.toString());
    }
    else {
        console.error("Instructie ontbreekt");
    }
}

function stap6(pakket: Vuurwerk[]): void {
    printHardeKnallers(pakket, 120);
}






function printHardeKnallers(pakket: Vuurwerk[], maxDecibel: number): void {
    console.log("--- STAP 6 ---");

    for (const vuurwerk of pakket) {
        if (vuurwerk instanceof Knaller && vuurwerk.decibel >= maxDecibel) {
            console.log(vuurwerk.toString());
        }
    }
}

// Start applicatie
main();

Hier is mijn Product.ts:

export abstract class Product {
    /**
     * Symbool voor Euro-teken
     */
    public static readonly EURO: string = "\u20AC";

    public _naam: string;
    public _prijs: number;

    public constructor(naam: string, prijs: number) {
        this._naam = naam;
        this._prijs = prijs;
    }

    public toString(): string {
        return `Naam: ${this._naam}\n\tPrijs: ${Product.EURO}${this._prijs.toFixed(2)}`;
    }

    public abstract isLegaal(): boolean;
}

Hier is mijn Instructie.ts:

/**
 * Deze class is al voor je gemaakt
 */

/**
 * Representeert een instructie voor het gebruik van vuurwerk.
 */
export class Instructie {
    public _nederlandstalig: boolean;
    public _minimumLeeftijd: number;
    public _omschrijving: string;
    

* Maakt een nieuwe instructie aan.
     * @param nederlandstalig - `true` als de instructie in het Nederlands is, anders `false`.
     * @param minimumLeeftijd - De minimale leeftijd om het vuurwerk te mogen gebruiken.
     * @param omschrijving - Een korte omschrijving van de instructie.
     */
    
public constructor(nederlandstalig: boolean, minimumLeeftijd: number, omschrijving: string) {
        this._nederlandstalig = nederlandstalig;
        this._minimumLeeftijd = minimumLeeftijd;
        this._omschrijving = omschrijving;
    }
    
    /** Geeft terug of de instructie Nederlandstalig is. */
    public get nederlandstalig(): boolean {
        return this._nederlandstalig;
    }
    
    /** Geeft de minimale leeftijd voor het vuurwerk. */
    public get minimumLeeftijd(): number {
        return this._minimumLeeftijd;
    }
    
    /** Geeft een stringrepresentatie van de instructie. */
    public toString(): string {
        return `Nederlandstalig=${this._nederlandstalig}, leeftijd=${this._minimumLeeftijd}, omschrijving=${this._omschrijving}`;
    }
}

Hier is mijn Vuurwerk.ts:

import { Instructie } from "./Instructie";
import { Product } from "./Product";

export class Vuurwerk extends Product {
    public _instructie?: Instructie;

    public constructor(naam: string, prijs: number, instructie?: Instructie) {
        super(naam, prijs);
        this._instructie = instructie;
    }

    public isLegaal(): boolean {
        return this._instructie !== undefined && this._instructie.nederlandstalig;
    }

    public toString(): string {
        return `Naam: ${this._naam}
        Prijs: ${Product.EURO}${this._prijs.toFixed(2)}
        Instructie: ${this._instructie ? this._instructie.toString() : "ontbreekt"}
        Legaal: ${this.isLegaal() ? "Ja" : "Nee"}`;
    }
}

Hier is mijn Knaller.ts:

import { Instructie } from "./Instructie";
import { Vuurwerk } from "./Vuurwerk";
import { Product } from "./Product";

export class Knaller extends Vuurwerk {
    public _aantalKnallen: number;
    public _decibel: number;

    public constructor(naam: string, prijs: number, aantalKnallen: number, decibel: number, instructie?: Instructie) {
        super(naam, prijs, instructie);
        this._aantalKnallen = aantalKnallen;
        this._decibel = decibel;
    }

    public isLegaal(): boolean {
        return this._instructie !== undefined && this._decibel < 120 && this._instructie._nederlandstalig;
    }
    
    public get decibel(): number {
        return this._decibel;
    }

    public toString(): string {
        return `Naam: ${this._naam}
        Prijs: ${Product.EURO}${this._prijs.toFixed(2)}
        Instructie: ${this._instructie ? this._instructie.toString() : "ontbreekt"}
        Legaal: ${this.isLegaal() ? "Ja" : "Nee"}
        Aantal knallen: ${this._aantalKnallen}
        Decibel: ${this._decibel}
        `;
    }
}

Hier is mijn Vuurpijl.ts:

import { Instructie } from "./Instructie";
import { Product } from "./Product";
import { Vuurwerk } from "./Vuurwerk";

export class Vuurpijl extends Vuurwerk {
    private _hoogte: number;
    private _kleurverhouding: number[];

    public constructor(naam: string, prijs: number, hoogte: number, kleurverhouding: number[], instructie?: Instructie) {
        super(naam, prijs, instructie);
        this._hoogte = hoogte;
        this._kleurverhouding = this._correcteKleurverhouding(kleurverhouding);
    }

    private correcteKleurverhouding(kleurverhouding: number[]): number[] {
        if (kleurverhouding[0] + kleurverhouding[1] + kleurverhouding[2] !== 100) {
            console.error ("Onjuiste kleurverhouding");
            return [100, 0, 0];
        }

        return kleurverhouding;
    }

    public isLegaal(): boolean {
        return this._instructie !== undefined && this._instructie.nederlandstalig && this._instructie._minimumLeeftijd > 16;
    }

    public toString(): string {
        return ` Naam: ${this._naam}
        Prijs: ${Product.EURO}${this._prijs.toFixed(2)}
        Intructie: ${this._instructie ? this._instructie.toString() : "ontbreekt"}
        Legaal: ${this.isLegaal() ? "Ja" : "Nee"}
        Hoogte: ${this._hoogte}
        Kleuren:
                ROOD: ${this._kleurverhouding[0]}%
                GROEN: ${this._kleurverhouding[1]}%
                BLAUW: ${this._kleurverhouding[2]}%
        `;
    }
}

本文标签: