简体   繁体   English

编写一个JavaScript函数,该函数返回包含您的姓名的字符串

[英]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 如果您需要一些用户交互,那么只需readlineNode.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.

相关问题 我需要创建一个 function 返回一个普通的 javascript 字符串,该字符串由“名称”索引,并包含所有“道具” - I need to create a function that returns a plain javascript string that is indexed by 'name', and contains all the 'props' 如何编写在JavaScript中以字符串形式返回脚本的函数 - How to write a function that returns a script as a string in javascript Javascript:返回承诺的写函数 - Javascript: write function that returns a promise 如何编写一个称为countDots(x)的函数,该函数将字符串作为参数并返回包含的点数(。)。 - How do I write a function called countDots(x) that takes a string as an argument and returns the number of dots (.) it contains 在javascript中将函数写为字符串 - Write function as a string in javascript 您如何编写一个从数组返回值的函数? - How do your write a function that returns values from an array? Javascript“写”为函数名称等 - Javascript “write” as function name and more javascript函数名称为字符串 - javascript function name as string 我必须写一个 javascript function 接受一个字符串,如果“字符串以元音开头和结尾”则返回 true - I have to write a javascript function that takes in a string and returns true if "the string starts and ends with a vowel" 用于确定类的名称中是否包含字符串的 Javascript - Javascript to determine if the class contains a string in its name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM