Загрузка...

Помогите с Readline

Тема в разделе Node.js создана пользователем wrathful_demon 6 авг 2022. 248 просмотров

  1. wrathful_demon
    wrathful_demon Автор темы 6 авг 2022 3 12 ноя 2021
    Всем привет!
    Начал учить Node.js, но не могу понять как сделать так, чтобы можно было вводить несколько вопросов и ответов. С Readline у меня получается получить только один ответ на один вопрос.

    const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
    })

    readline.question(`Как тебя зовут?`, (name) => {
    console.log(`Привет, ${name}!`)
    })
    readline.question(`Сколько тебе лет?`, (age) => {
    console.log(`Привет, ${name}, Тебе ${age}!`)
    readline.close()
    })


    Вот это максимально не правильный код, помогите разобраться, пожалуйста.
     
  2. pinilopa
    pinilopa 6 авг 2022 Заблокирован(а) 69 17 апр 2021
    JavaScript
    const client = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
    })

    const questions = [
    'What is your name?',
    'What is your favorite color?',
    'What is your favorite animal?',
    'What is your favorite food?',
    'What is your favorite sport?',
    'What is your favorite movie?',
    ]

    const answers = {}

    client.on('line', (line) => {
    if (line === 'exit') {
    client.close()
    } else {
    const question = questions.shift()
    answers[question] = line
    if (questions.length) {
    client.write(question)
    } else {
    client.close()
    }
    }
    }).on('close', () => {
    console.log(answers)
    }).write(questions.shift())
    --- Сообщение объединено с предыдущим 6 авг 2022
     
    1. wrathful_demon Автор темы
      pinilopa, Огромное спасибо!
Загрузка...
Top