ask the user to put in a list of numbers. then print out the list in typescript

To ask the user for a list of numbers and print out the list in TypeScript, you can use the following code:

index.ts
const readline = require("readline");

// Create interface for user input
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

// Ask user for a list of numbers separated by commas
rl.question("Enter a list of numbers separated by commas: ", (input: string) => {

  // Convert user input string to array of numbers
  const numbers = input.split(",").map((num) => parseFloat(num.trim()));

  // Print out the list of numbers in the console
  console.log(`List of numbers: ${numbers}`);
  
  // Close the interface
  rl.close();

});
576 chars
22 lines

First, we create an interface for user input using the readline module. Then we ask the user to enter a list of numbers separated by commas using rl.question(). When the user inputs the list, we split the input string into an array of numbers using split() and map(). Finally, we print out the list of numbers in the console using console.log().

gistlibby LogSnag