Create a TypeScript program that prompts the user to enter the length and width of a rectangle, then calculates and displays the area of the rectangle. Ensure that the program uses appropriate variable types, handles user input errors, and outputs the result in a clear and readable format.
Create a TypeScript program that prompts the user to enter the length and width of a rectangle, then calculates and displays the area of the rectangle. Ensure that the program uses appropriate variable types, handles user input errors, and outputs the result in a clear and readable format. The user attention ratio is 0.2301025390625.
user
Here's an example TypeScript program for calculating the area of a rectangle:

```typescript
const length: number = parseFloat(prompt("Enter the length of the rectangle:"));
const width: number = parseFloat(prompt("Enter the width of the rectangle:"));

if (isNaN(length) || isNaN(width)) {
  console.log("Please enter valid numbers for length and width.");
} else {
  const area: number = length * width;
  console.log(`The area of the rectangle is ${area} square units.`);
}
```

In this program, we define two variables `length` and `width` as numbers, and prompt the user to enter values for them using `prompt()`. We use `parseFloat()` to convert the input strings to numbers.

We then check if either `length` or `width` is `NaN` (not a number), which indicates that the user entered an invalid input. If this is the case, we output a message asking the user to enter valid numbers.

If both `length` and `width` are valid numbers, we calculate the area of the rectangle by multiplying them together, and output the result in a template string using `console.log()`. The output is in a clear and readable format, indicating the units (square units) for clarity.
assistant
Can you provide additional examples of how to calculate the area of different shapes using TypeScript? I want to expand my knowledge on this topic. The user attention ratio is 0.1365966796875.
user
Sure, here are some examples of how to calculate the area of different shapes using TypeScript:

1. Triangle
```typescript
const base: number = parseFloat(prompt("Enter the base of the triangle:"));
const height: number = parseFloat(prompt("Enter the height of the triangle:"));

if (isNaN(base) || isNaN(height)) {
  console.log("Please enter valid numbers for base and height.");
} else {
  const area: number = 0.5 * base * height;
  console.log(`The area of the triangle is ${area} square units.`);
}
```

2. Circle
```typescript
const radius: number = parseFloat(prompt("Enter the radius of the circle:"));

if (isNaN(radius)) {
  console.log("Please enter a valid number for radius.");
} else {
  const area: number = Math.PI * radius * radius;
  console.log(`The area of the circle is ${area} square units.`);
}
```

3. Square
```typescript
const side: number = parseFloat(prompt("Enter the length of a side of the square:"));

if (isNaN(side)) {
  console.log("Please enter a valid number for the side.");
} else {
  const area: number = side * side;
  console.log(`The area of the square is ${area} square units.`);
}
```

4. Trapezoid
```typescript
const base1: number = parseFloat(prompt("Enter the length of the first base:"));
const base2: number = parseFloat(prompt("Enter the length of the second base:"));
const height: number = parseFloat(prompt("Enter the height of the trapezoid:"));

if (isNaN(base1) || isNaN(base2) || isNaN(height)) {
  console.log("Please enter valid numbers for bases and height.");
} else {
  const area: number = 0.5 * (base1 + base2) * height;
  console.log(`The area of the trapezoid is ${area} square units.`);
}
```

These examples all follow a similar pattern to the rectangle example, where we prompt the user for necessary values, validate the input, calculate the area using appropriate formulas, and output the result in a clear and readable format.
assistant