0

I am right now using the API of yodaspeak And I made some small changes on the codes. I want the user to input what words to change here is my node.js code

var YodaSpeak = require('yoda-speak');
var yoda = new YodaSpeak('hZy4MAVwtbmshFTdDuJSiMxKIxWsp1JSor1jsnuNeLjEVTnbXv');

console.log("Please enter the sentence you want to 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 always tells error message I need to how to get the user input and convert it to string and assign it to a variable.

BTW in advance we need to install yoda-speak

%npm install --save yoda-speak

the error message is

Please enter the sentence you want to convert
fjesil
[object undefined].  
J.Zhou
  • 27
  • 1
  • 7
  • *"the console always tells error message"* What error message? What line did it happen on? what file? – Kevin B Jul 14 '15 at 20:14
  • oh Sorry I will upload it right away. The Problem that I am having is that I don't know how to make a prompt and replace the string in yoda.convert with user input – J.Zhou Jul 14 '15 at 20:24
  • i don't understand your code... `words` is defined as undefined, and then you use `words` as the name of an event you're listening to, so that event handler shouldn't ever run. – Kevin B Jul 14 '15 at 20:36
  • Additionally, you're calling yoda.convert way too early. see the documentation: https://nodejs.org/api/process.html#process_process_stdin – Kevin B Jul 14 '15 at 20:37
  • I mean, I want to name the sentence user inputted "words" and then use it for converting. It is like the same sctructure as the reference page one. – J.Zhou Jul 14 '15 at 20:48
  • in the reference page, there is a string in the place of words – J.Zhou Jul 14 '15 at 20:51
  • I'm talking about this line: `process.stdin.on(words` I'm completely ignoring the yodaspeak portion of your question at the moment, because the part before it isn't working yet. – Kevin B Jul 14 '15 at 20:51
  • Ok, I am sorry that I still cannot understand it will, I read the reference but still don't know how to output the input as a variable or a string. Sorry – J.Zhou Jul 14 '15 at 20:54
  • For example, if you run this code, you get undefined before even entering any text. http://pastebin.com/jpkCxzdz as expected, because you need an on data listener and an on end listener. – Kevin B Jul 14 '15 at 20:56
  • I modified my code and attached it as the second version – J.Zhou Jul 14 '15 at 21:03

1 Answers1

0

yodaspeak isn't where your problem lies, it's with how you're accepting text from the console. process.stdin.on( is adding an event listener, and the event you're interested in is named data, not undefined. Below is how you would console.log() each word that is input in the console:

console.log("Please enter the sentence you want to convert");
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (words) {
    console.log(words);
});

Now all you have to do is tie yodaspeak back into it.

console.log("Please enter the sentence you want to 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);
        }
    });
});
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • Oh, Thanks a lot, it seems that I need make up on my basis of programming, My problem is solved. Thank you very much – J.Zhou Jul 14 '15 at 21:09
  • You can omit the `.resume()` call if you're using a more recent version of node. – Kevin B Jul 14 '15 at 21:10