-1

I am supposed to finish the provided function 'climb' shown below. Using the built-in local variable arguments, within the function climb.

This is what the function is 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."

Has to pass 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 provided function:

function climb(){

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

}

This is what I am trying and it doesn't work:

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.";
  }
}
stoney_24
  • 125
  • 2
  • 13
  • 2
    The phrase *"built-in local variable arguments"* doesn't make any sense. If they're local variables, they aren't arguments. – T.J. Crowder May 29 '18 at 17:22
  • 2
    Nothing in your quoted code is doing anything to test the conditions you describe in the text...? Assignments aren't usually arbitrary; your instructor, tutorial, or course will have covered the necessary topics to make it possible for you to do this. I suggest you **review your course materials, class notes, etc.,** and try again to do the work. *If* you run into a *specific* problem, research it thoroughly, [search thoroughly here](/help/searching), and if you're still stuck post your code and a description of the problem. People will be glad to help. – T.J. Crowder May 29 '18 at 17:23
  • 1
    I sure hope arguments[0] equals itself. – epascarello May 29 '18 at 17:24
  • 1
    @epascarello - It *usually* will. If it's `NaN`, it won't. But of course, above it's `''`, so... – T.J. Crowder May 29 '18 at 17:26
  • 1
    We don't use `arguments` any more. The only reason we ever used it was for "variadic" functions with variable numbers of arguments, but now we use spread parameters (`...args`) for that. Why is your teacher asking you to do an assignment that involves `arguments`, instead of just using named parameters? I suggest finding a new teacher, a new class, or possibly a new university. If you are going to use `arguments`, I strongly recommend you learn what it means. It's not something you set, as you are doing; it represents the parameters **passed in** to the function. –  May 29 '18 at 17:34

3 Answers3

0

arguments[0] == arguments[0] is always going to be true because you are checking if it is equal to itself. Also to check if an element is true you just do if(element) You code should instead look like this:

if(arguments[0] != '') {
  if(arguments[1]){
    return "On belay?";
   }
   else {
    return "Climbing";
   }

} else {
  return "Let's set up the belay rope before we climb."
}
Sacha
  • 322
  • 1
  • 8
  • 2
    I regret having to be "that guy" but if `arguments[0]` happens to be `NaN` then the comparison will fail. – Pointy May 29 '18 at 17:25
  • 1
    You're right, I'm assuming it won't since OP sets it to `''` at the beginning – Sacha May 29 '18 at 17:26
  • @Pointy - LOL, I was just [that guy](https://stackoverflow.com/questions/50589726/what-do-i-need-to-do-to-make-this-if-else-statement-work-properly#comment88188925_50589726). – T.J. Crowder May 29 '18 at 17:27
  • @Sacha This only returns the last statement. The other two do not work – stoney_24 May 29 '18 at 17:35
  • Probably because you are never setting `arguments[0]` and `arguments[1]` – Sacha May 29 '18 at 17:37
0

If I understand you correctly, your function needs to work based on local variables, not input arguments.

That being said, your comparisons don't make much sense. E.g. arguments[0] == arguments[0] should always return true, as it is being compared to itself.

Try something like

//check that arg[0] has a value (assuming it is a string)
 if(arguments[0]){
   if(arguments[1]==false){
     return "On belay?";
   } else { //arguments[1] == false is implicit here
     return "Climbing";
   }
 } else {
   return "Let's set up the belay rope before we climb.";
 }

Also take note of the difference between != (not equal) and !== (not equal value or not equal type).

Falk
  • 45
  • 6
  • Thank you. This almost works. It just doesn't pass one of my required tests. It also needs to return "On belay?" if called with climb("any string"). – stoney_24 May 29 '18 at 17:54
  • Is it possible to make a small change to make that work? – stoney_24 May 29 '18 at 18:45
  • I thought you did not want any parameters for the function? Or do you want to return "on belay" if there is only a first argument and no second? I'm not sure I understand your requirement correctly. – Falk May 29 '18 at 19:30
  • You are correct I can't have parameters for the function but I am meant to pass 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") - – stoney_24 May 29 '18 at 19:33
  • Please check your initial question against what you just wrote because they are contradictory: Question: _I am to write a function using built-in local variable arguments._ Comment: _should be a function that does not have built-in parameters_ Edit your question with the correct one, which I suppose is the second. – Falk May 29 '18 at 19:58
  • I edited the question to show exactly what I am supposed to do. – stoney_24 May 29 '18 at 20:30
  • Your updated question looks like this should involve input parameters. In other languages or without your specifications I would simply solve this with overloading (3 defined functions climb(), climb(String a) and climb (String a, String b)). As torazaburo pointed out, the use of arguments is strange, and should be avoided here. To fix your question anyway, try `if(typeof arguments[1] !== "undefined" || arguments[1] == false)` as the inner if condition (src: https://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices/9298062#9298062) – Falk May 30 '18 at 13:09
  • I agree that the use of arguments is also very strange. That's why I am so confused by this particular problem. I have never come across using the built in arguments. Unfortunately this `if(typeof arguments[1] !== "undefined" || arguments[1] == false)` does not work either. :( – stoney_24 May 30 '18 at 15:17
  • You should try a `console.log(typeof arguments[1])` (or any form of output available to you) with `climb("any string")` to find out what you get if the second argument is not given and then react to that. – Falk Jun 01 '18 at 09:15
0

if(arguments[0]==arguments[0]) will forever be true no matter what arguments[0] is.

To check if arguments[0] has a string you can use typeof and length.

if(typeof arguments[0] === "string" && arguments[0]+"".length){
    //runs this, if arguments[0] is a string with data in it.
}

this will check if it is a string and if it has anything in it.

To check if arguments[1] is false you could use if(!arguments[1]) but then will still run true if arguments[1] is undefined or an empty string. I would suggest using a more readable and secure way.

if(arguments[1]===false){
    //runs this, if arguments[1] is false
}