[英]write a JavaScript function that returns a string that contains your name
So if my name is Ryan, I need to use a function to produce Ryan back to me. 因此,如果我的名字叫Ryan,我需要使用一个函数将Ryan反馈给我。
In class, we did 在课堂上,我们做到了
function readnumber()
{
var s = readline();
return (s * 1);
}
We weren't taught anything else, so I'm really confused. 我们什么都没教,所以我真的很困惑。
How about if I need to use a function to take a number and add 10 to it? 如果我需要使用一个函数来取一个数字并将其加10,该怎么办?
Easiest way for someone named Ryan is simply 名为Ryan的人的最简单方法就是
function foo() {
return "Ryan";
}
So you would then have 这样你就会有
foo(); // "Ryan"
If you require some user interaction, then a simple modification of an example from Node.js's documentation on readline
gives you 如果您需要一些用户交互,那么只需对
readline
上Node.js文档中的示例进行简单修改即可
var readline = require('readline'),
rememberedName = null; // a variable to hold the name
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What is your name? ", function(answer) {
console.log("Hello", answer); // great we now know the name
rememberedName = answer; // store it so a function can return it
rl.close();
});
// then after
function foo() { // a function which returns the name
return rememberedName;
}
Perhas something like this will help you: 像这样的东西将帮助您:
function namePrompt()
{
var name = prompt("What's your name?");
alert("Hello " + name);
}
Th result will prompt the user for their name. 结果将提示用户输入他们的名字。 After input, a dialog box will pop up saying "Hello " together with the user's name.
输入后,将弹出一个对话框,说“ Hello”以及用户名。
If you're using Node.js, change alert
to Console.log
. 如果您使用的是Node.js,请将
alert
更改为Console.log
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.