Answer to Question #296146 in HTML/JavaScript Web Application for chethan

Question #296146

Car Race

the goal of this code is to get output using with Classes.

input

first line of input contains a string playerName

second line of input contains a number nitro

third line of input contains a number speed

output

the output contains multiple lines with the appropriate texts

input1

Alyssa

100

1

output1

Race has started

Speed 50; Nitro 50

speed 70; Nitro 60

Alyssa is the winner

Speed 0; Nitro 60


function readLine() {

 return inputString[currentLine++];

}

 // Create your Super Class Race and Sub Class Car here


function main() {

 const playerName = readLine();

 const nitro = JSON.parse(readLine());

 const speed = JSON.parse(readLine());


 const car1 = new Car(playerName, nitro, speed);


 console.log(car1.startRace());

 car1.nitroBoost();

 console.log(`Speed ${car1.speed}; Nitro ${car1.nitro}`);

 car1.accelerate();

 console.log(`Speed ${car1.speed}; Nitro ${car1.nitro}`);

 console.log(car1.endRace());

 car1.applyBreak();

 console.log(`Speed ${car1.speed}; Nitro ${car1.nitro}`);

}


1
Expert's answer
2022-02-13T09:44:54-0500
function readLine() {
    return inputString[currentLine++];
}
    
    // Create your Super Class Race and Sub Class Car here
class Car {

    constructor(playerName, nitro, speed) {
        this.playerName = playerName;
        this.nitro = nitro;
        this.speed = speed;
    }

    startRace() {
        return 'Race has started'
    }

    nitroBoost() {
        this.nitro -= 50
        this.speed += 50
    }

    accelerate() {
        this.nitro += 10
        this.speed += 20
    }
    
    endRace() {
        return `${this.playerName} is the winner`
    }

    applyBreak() {
        this.speed = 0
    }
}
    
function main() {

    const playerName = readLine();
    const nitro = JSON.parse(readLine());
    const speed = JSON.parse(readLine());

    const car1 = new Car(playerName, nitro, speed);

    console.log(car1.startRace());
    car1.nitroBoost();
    console.log(`Speed ${car1.speed}; Nitro ${car1.nitro}`);
    car1.accelerate();
    console.log(`Speed ${car1.speed}; Nitro ${car1.nitro}`);
    console.log(car1.endRace());
    car1.applyBreak();
    console.log(`Speed ${car1.speed}; Nitro ${car1.nitro}`);


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS