【问题标题】:Simple Assignment Help: Calling the function to pass it a parameter and to get its return value简单赋值帮助:调用函数向其传递参数并获取其返回值
【发布时间】:2020-01-29 00:18:46
【问题描述】:

Assignment HELP:“调用函数向其传递参数并获取其返回值”

您好,我是一个刚开始在 Uni 中使用 javascript 的新手,需要帮助我完成关于“调用函数以向其传递参数并获取其返回值”的这部分作业

这些是作业的详细说明:

  1. 用参数和返回值声明一个函数 • 在标签内,添加一个名为 getInput() 的函数,并带有一个名为 (request) 的参数。

给它两个陈述。

  • 第一条语句提示用户输入任何请求,并将其存储在名为 message 的变量中。

-第二条语句将消息返回到调用函数的地方

  1. 调用函数向其传递参数并获取其返回值

现在添加一个名为 testParamsReturn() 的函数

•声明一个名为“firstName”的变量

•在一行中: - 调用 getInput() 函数 - 为调用提供“名字”参数(作为字符串)。 - 将函数返回的值赋给 firstName。

•声明一个名为“secondName”的变量

•在一行中: - 调用 getInput() 函数 - 给调用一个“第二个名字”的参数 - 将函数返回的值分配给“secondName”。

警告“Testing a function with parameters and a return value of”,并附加您在​​函数中声明的两个变量

<html>
<head></head>
<body>
<section>
<input type="button" value="Function with Parameters and Return Value"     onClick="testParamsReturn()">
</section>
<script>
function getInput(request){
var message = "Enter details here";
return message;
}
function testParamsReturn(){
var firstName = "Name1";
function getInput("first_name") return firstName;
var secondName = "Name2";
function getInput("second_name") return secondName;
alert ("Testing a function with parameters and a return value of" + firstName + secondName);
} 

其他部分工作正常,但此功能使我无法单击文件中的任何按钮。

【问题讨论】:

  • function getInput("first_name") return firstName; 你不能在参数中设置字符串,可能你的意思是function getInput(firstName) return "first_name";

标签: javascript html function scope return


【解决方案1】:

我在代码中描述的所有步骤,希望对您有所帮助;)

<html>
  <head></head>
  <body>
    <section> 
      <h1> on page must be H1 tag, in section must be H1-6 tag.</h1>
      <input type="button" value="Function with Parameters and Return Value" onclick="testParamsReturn()">
    </section>
  </body>
  <script>
    // Inside the tags
    
    /* Declaring a function with parameters and return value   */
    // add a function named getInput() with a parameter named (request).
    function getInput(request){
      // The first statement prompts the user for whatever the request
      // is and stores it in a variable called message.
      var message = prompt(request);
      
      // The second statement returns the message
      return message;
    }
    
    // Now add a function named testParamsReturn() that
    function testParamsReturn(){
      // •Declares a variable named "firstName"
      var firstName;
      
      // In a single line: -Calls the getInput() function -Gives the call a parameter of “first name” (as a string). -Assigns the value returned by the function to firstName.
      firstName = getInput('first name');

    
      // •Declares a variable named "secondName"
      var secondName;
      
      // •In a single line: -Calls the getInput() function -Gives the call a parameter of “second name” -Assigns the value returned for the function to "secondName".
      secondName = getInput('second name');

      // Alerts
      alert (
        // “Testing a function with parameters and a return value of ”
        "Testing a function with parameters and a return value of " + 
        // appends both the variables you have declared in the function
        firstName + 
        ' ' +
        secondName
       );
    } 
  </script>
</html>

【讨论】:

    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2019-06-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多