find the largest number in the following list of numbers: [1, 3, 4, 2, 5] in typescript

To find the largest number in an array of numbers in TypeScript, we can use a simple for loop to iterate over each number in the array and compare it to the current largest number.

Here's the code to find the largest number in [1, 3, 4, 2, 5]:

index.ts
const numbers: number[] = [1, 3, 4, 2, 5];
let largestNumber: number = numbers[0];

for (let i = 1; i < numbers.length; i++) {
  if (numbers[i] > largestNumber) {
    largestNumber = numbers[i];
  }
}

console.log('The largest number is:', largestNumber);
256 chars
11 lines

In this code, we start by initializing the numbers array and a largestNumber variable to the first item in the array.

We then use a for loop to iterate over each of the remaining numbers in the array, starting at index 1. We compare each number to the current largest number using a conditional (if) statement and update largestNumber if the current number is larger.

Finally, we output the result to the console using console.log().

The output of this code is:

index.ts
The largest number is: 5
25 chars
2 lines

gistlibby LogSnag