Skip to content
Advertisement

Print error for missing argument

I am trying to write a script which will check number of arguments for first and second number; if both variable entered, it will do the calculation; if one argument is missing, it will print error message. Here what I’ve done so far:

JavaScript

I am always getting error message even though I enter both of the numbers. What am I missing? Please help.

Advertisement

Answer

Test Your Assigned Variables, Not Positional Parameters

Your variables num1 and num2 aren’t positional parameters, and so the special parameter $# is probably not what you think it is. You should change your conditional to check that both variables are set. For example:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement