219k views
4 votes
Write the code for the function below. The function is passed a speed in MPH and returns 0,1 , or 2 for the ticket type. There are 3 possibilities for tickets

User Pafjo
by
8.5k points

1 Answer

5 votes

Final answer:

To write the code for the function, you can use conditional statements to check the speed and return the appropriate ticket type.

Step-by-step explanation:

To write the code for the function, you can use conditional statements to check the speed and return the appropriate ticket type. Here's an example:

function getTicketType(speed) {
if (speed <= 60) {
return 0; // No ticket
} else if (speed <= 80) {
return 1; // Speeding ticket
} else {
return 2; // Reckless driving ticket
}
}

In this code, the function takes in a speed parameter and uses if statements to check the speed against different thresholds. If the speed is <= 60, it returns 0 indicating no ticket. If the speed is <= 80, it returns 1 indicating a speeding ticket. Otherwise, it returns 2 indicating a reckless driving ticket.

User Karan Sharma
by
8.5k points