2

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!

}

Here is what I am trying:

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.";
  }
}

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

1
  • The key to this is how you’re checking for a “falsy” value. Commented May 30, 2018 at 20:46

1 Answer 1

1

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.";
  }
}

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


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


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.";
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much! I knew it was that if statement that I needed to tweak. I just wasn't exactly sure what I need to do. I had tried something pretty similar to this before asking.
@stoney_24 Glad I could help! typeof has helped me countless times with similar issues too. Really nifty operator.
This is a little unnecessary - All you need to do is if(!arguments[1]), which will simply check if arguments[1] is falsy as described in the requirements.
@KirkLarkin Agreed. Also, you won't need two conditions for that. Please add it as an answer and I'll upvote it man. Cheers.
No problem. Feel free to just add it to your own answer as an improvement or alternative.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.