0

Probably titled this one badly, will try and explain a bit better.

Basically I've been trying to make a function:

var readlineSync = require('readline-sync');
function i(context) {
  readlineSync.question(context)
} 

var Username = i("Testing the prompt: ") 
console.log(Username)  

I find having to write readlineSync.question over and over again rather irritating, but running the code returns this:

Testing the prompt: Hello
undefined

Am I doing something wrong?

3
  • you do know write isn't spelt wright ? Commented May 16, 2016 at 17:44
  • No? its correct... Commented May 18, 2016 at 8:49
  • yes, I made a typo. My bad. Before the edit. Commented May 18, 2016 at 8:51

3 Answers 3

3

You're not returning anything from the function.

It should be:

function i(context) {
  return readlineSync.question(context)
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, that would make sense. Thank you!
2

You could just do:

var i = readlineSync.question

// usage
i('Testing the prompt: ')

Creating an alias of the function

Or, if you are using an ES6 capable environment (Node 6 or Chrome):

import { question as i } from 'readline-sync'

// usage
i('Testing the prompt: ')

Which is the same of:

var i = require('readline-sync').question

// usage
i('Testing the prompt: ')

1 Comment

Ahh, thank you! I have been trying to figure out how to do the exact thing.
1

you forgot the return statement

function i(context){
 return readlineSync.question(context)
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.