javascript - How to read user input and convert it to a string -


i right using api of yodaspeak , made small changes on codes. want user input words change here node.js code

var yodaspeak = require('yoda-speak'); var yoda = new yodaspeak('hzy4mavwtbmshftddujsimxkixwsp1jsor1jsnuneljevtnbxv');  console.log("please enter sentence want convert:"); process.stdin.on('readable', function() { var chunk = process.stdin.read(); if (chunk !== null) {   yoda.convert(tostring(chunk), function(err, result) { if (!err) {     console.log(result.tostring()); } else {     console.log(err); } })   } }); 

the console tells error message need how user input , convert string , assign variable.

btw in advance need install yoda-speak

%npm install --save yoda-speak 

the error message

please enter sentence want convert fjesil [object undefined].   

yodaspeak isn't problem lies, it's how you're accepting text console. process.stdin.on( adding event listener, , event you're interested in named data, not undefined. below how console.log() each word input in console:

console.log("please enter sentence want convert"); process.stdin.setencoding('utf8'); process.stdin.on('data', function (words) {     console.log(words); }); 

now have tie yodaspeak it.

console.log("please enter sentence want convert"); process.stdin.setencoding('utf8'); process.stdin.on('data', function (words) {     yoda.convert(words, function (err, result) {         if (!err) {             console.log(result.tostring());         } else {             console.log(err);         }     }); }); 

Comments