英文:
Running more than one function in Node.js
问题
I'm having trouble understanding why my application will terminate after running only one function. I have coded the application as follows...
const readline = require('readline');
const io = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('Welcome. This application will check if a given year is a leap year.');
checkYear();
function checkYear() {
io.question('Check which year? ', function (year) {
year % 100 === 0 && year % 400 !== 0
? false
: year % 4 === 0
? console.log(year + ' is a leap year.')
: console.log(year + ' is not a leap year.');
});
io.question('Check another year? y/n'), function(response) {
if (response.toLowerCase === 'y') {
checkYear();
} else {
io.close();
}
}
};
我也尝试将第二个提示分成自己的函数,并连续调用checkYear和checkAgain,但我得到了相同的结果。我需要做什么来使io.question('Check another year')提示在应用程序响应第一个输入后运行?
英文:
I am having trouble understanding why my application will terminate after running only one function. I have coded the application as follows...
const readline = require('readline');
const io = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(
'Welcome. This application will check if a given year is a leap year.'
);
checkYear();
function checkYear() {
io.question('Check which year? ', function (year) {
year % 100 === 0 && year % 400 !== 0
? false
: year % 4 === 0
? console.log(year + ' is a leap year.')
: console.log(year + ' is not a leap year.');
});
io.question('Check another year? y/n'), function(response) {
if (response.toLowerCase === 'y') {
checkYear();
} else {
io.close();
}
}
};
I have also tried breaking the second prompt into its own function and calling both checkYear and checkAgain back-to-back, but I get the same result. What do I need to do to make the io.question('Check another year') prompt run after the application responds to the first input?
答案1
得分: 2
I believe you are expecting another input with 2nd io.question
call.
In your code, 2nd io.question
will be executed immediately after the 1st io.question
without waiting for the user to enter anything.
So, you will need to nest the 2nd call inside the 1st call:
function checkYear() {
io.question("Check which year? ", function (year) {
year % 100 === 0 && year % 400 !== 0
? false
: year % 4 === 0
? console.log(year + " is a leap year.")
: console.log(year + " is not a leap year.");
io.question("Check another year? y/n", function (response) {
if (response.toLowerCase() === "y") {
checkYear();
} else {
io.close();
}
});
});
}
Other than that, a couple of typo mistakes are:
- should be
io.question("Check another year? y/n", function (response) {
...
});
NOT
io.question('Check another year? y/n'), function(response) {
...
}
- should be
response.toLowerCase() === "y"
NOT
response.toLowerCase === 'y'
You can refer to my sandbox: https://codesandbox.io/p/sandbox/friendly-lake-b15kkz
Hope this helps
英文:
I believe you are expecting another input with 2nd io.question
call.
In your code, 2nd io.question
will be executed immediately after the 1st io.question
without waiting for user to enter anything.
So, you will need to nest the 2nd call inside the 1st call:
function checkYear() {
io.question("Check which year? ", function (year) {
year % 100 === 0 && year % 400 !== 0
? false
: year % 4 === 0
? console.log(year + " is a leap year.")
: console.log(year + " is not a leap year.");
io.question("Check another year? y/n", function (response) {
if (response.toLowerCase() === "y") {
checkYear();
} else {
io.close();
}
});
});
}
Other than that, couple of typo mistakes are:
- should be
io.question("Check another year? y/n", function (response) {
...
});
NOT
io.question('Check another year? y/n'), function(response) {
...
}
- should be
response.toLowerCase() === "y"
NOT
response.toLowerCase === 'y'
You can refer to my sandbox: https://codesandbox.io/p/sandbox/friendly-lake-b15kkz
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论