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.ts256 chars11 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.ts25 chars2 lines
gistlibby LogSnag