Skip to content Skip to sidebar Skip to footer

How To Write A Function Using The Built-in Local Variable Arguments?

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 '

Solution 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:

functionclimb(){

  //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:

functionclimb(){

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

Post a Comment for "How To Write A Function Using The Built-in Local Variable Arguments?"