0

This is my piece of code and it is not working in the expected way. Can give me any idea what i have wrong

<html>
<body>
<form>
  <input type="submit" value="Check" onclick="f(x, y, z)"/>
</form>
</body>
<script type="text/javascript">

var x = prompt("Enter the number",""); 
var y = prompt("Enter the number","");
var z = prompt("Enter the number","");

function f(x, y, z)
{
    // first, check that the right # of arguments were passed.
    if (f.arguments.length != 3) {
        alert("function f called with " + f.arguments.length +
              "arguments, but it expects 3 arguments.");
        return null;
    }


}
</script>
</html>
2
  • In the future, you should say what the problem is. What did you expect and what is happening. Commented Oct 10, 2013 at 12:11
  • Hi, at the first, i am checking the arguments that was passed is matching the 3 arguments or not. But, still, it is not working.. Any idea? Commented Oct 10, 2013 at 12:17

3 Answers 3

3

the function you are calling will always have 3 arguments. maybe you want to check if the arguments are not empty?

if (x=='' || y=='' || z==''){}
Sign up to request clarification or add additional context in comments.

Comments

2

You should be checking arguments.length, not f.arguments.length

Edit: @TheMobileMushroom also pointed out that arguments length will be 3 even if the args are empty strings. You can change it to

if (!x || !y || !z)

Don't use arguments.length

5 Comments

Hi dude, I am still facing the same problem. Any idea?
What is the problem? Is the function being called? What are the values of x, y and z?
I am getting the value from the user through 'prompt'. Please see my code. For the function f(), i am getting the values from user.
I mean what behavior are you actually seeing? What apart of it is not working? Are you even getting into the function call?
I am expecting the alert box when i am missing to give 3 arguments. But, I am not getting any alert when i dont enter exactly 3 arguments.
0

Try to place the script tag before the form tag, so the x,y and z will be declared before.

Comments

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.