To find the largest number among a list of numbers in a Node.js program, you can use a simple loop to iterate through the list and keep track of the maximum value encountered. Here’s an example program to do that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Sample list of numbers const numbers = [10, 5, 20, 8, 15, 30, 25]; // Initialize a variable to store the maximum number, assuming the first number is the maximum let maxNumber = numbers[0]; // Iterate through the list of numbers for (let i = 1; i < numbers.length; i++) { // Check if the current number is greater than the current maximum if (numbers[i] > maxNumber) { // If it is, update the maximum number maxNumber = numbers[i]; } } // Print the maximum number console.log("The maximum number is: " + maxNumber); |
Replace the numbers
array with your own list of numbers to find the largest among them. When you run this Node.js program, it will output the maximum number from the list.
Here’s how you can run the program:
- Save the code to a file with a
.js
extension (e.g.,findMaxNumber.js
). - Open your terminal or command prompt.
- Navigate to the directory where the file is located.
- Run the program with Node.js:
1 |
node findMaxNumber.js |
It will display the largest number from the list.