如何使用内置的局部变量参数编写函数? [英] How to write a function using the built-in local variable arguments?

查看:82
本文介绍了如何使用内置的局部变量参数编写函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javascript还是很陌生,所以如果这很烦人,我深感抱歉.因此,我遇到了一个我不熟悉并试图解决的非常奇怪的问题.我要完成下面列出的爬升"功能.我需要在函数攀爬内使用内置的局部变量参数.我不能更改函数以接受参数,此练习的目的是在函数范围内使用局部参数变量.任何帮助是极大的赞赏!预先感谢.

I am still very new to javascript so I apologize if this is annoying. So I have this really odd problem that I am unfamiliar with and trying to solve. I am to finish the function 'climb' which I have listed below. I am required to use the built-in local variable arguments, within the function climb. I can NOT alter the function to take in parameters, the purpose of this exercise is to use the local arguments variable within the function scope. Any help is greatly appreciated! Thanks in advance.

此功能必须执行的操作:

This is what the function has to do:

If there is a string at arguments[0] but arguments[1] is falsy, return "On belay?". 

If there is a string at arguments[0], and true at arguments[1],
return "Climbing!"

Otherwise, return "Let's set up the belay rope before we climb."

它还必须通过所有这些测试:

It also has to pass all of these tests:

should be a function that does not have built-in parameters
should return "Let's set up the belay rope before we climb." if called as climb()
should return "Climbing!" if called with climb("Benny", true)
should return "Climbing!" if called with climb("any string here", true)
should return "On belay?" if called with climb("Benny", false)
should return "On belay?" if called with climb("any string here")

这是我提供的功能的开始:

Here is the start of the function I am provided with:

function climb(){

  //CODE HERE - DO NOT TOUCH THE CODE ABOVE!

}

这是我正在尝试的:

function climb(){

  //CODE HERE - DO NOT TOUCH THE CODE ABOVE!

  if(arguments[0]){
    if(arguments[1]==false){
      return "On belay?";
    } else { 
      return "Climbing!";
    }
  } else {
    return "Let's set up the belay rope before we climb.";
  }
}

这通过了除该测试以外的所有测试: should return "On belay?" if called with climb("any string here")

This passes all the tests except for this one: should return "On belay?" if called with climb("any string here")

推荐答案

您可以使用

You can add a second condition to the relevant if statement to check if it has a boolean (true/false) or not by using the typeof operator like this:

function climb(){

  //CODE HERE - DO NOT TOUCH THE CODE ABOVE!

  if(arguments[0]){
    if(arguments[1]==false || (typeof(arguments[1]) != typeof(true))){
      return "On belay?";
    } else { 
      return "Climbing!";
    }
  } else {
    return "Let's set up the belay rope before we climb.";
  }
}

在上面,typeof检查argument[1]是否为boolean类型的值.

In the above, typeof checks if argument[1] is a value of type boolean or not.

jsFiddle:: https://jsfiddle.net/AndrewL64/k8ykedz3/

如@KirkLarkin所述,一种更简短的方法是使用!来检查它是否虚假:

As @KirkLarkin mentioned, a shorter and cleaner approach would be to use the ! to check if it's falsy or not like this:

function climb(){

  //CODE HERE - DO NOT TOUCH THE CODE ABOVE!

  if(arguments[0]){
    if(!arguments[1]){
      return "On belay?";
    } else { 
      return "Climbing!";
    }
  } else {
    return "Let's set up the belay rope before we climb.";
  }
}

这篇关于如何使用内置的局部变量参数编写函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆