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>
Snake Eyes
16.8k39 gold badges120 silver badges235 bronze badges
asked Oct 10, 2013 at 12:09
Sakthivel
6662 gold badges6 silver badges22 bronze badges
-
In the future, you should say what the problem is. What did you expect and what is happening.Jeff Storey– Jeff Storey2013年10月10日 12:11:57 +00:00Commented 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?Sakthivel– Sakthivel2013年10月10日 12:17:24 +00:00Commented Oct 10, 2013 at 12:17
3 Answers 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
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
answered Oct 10, 2013 at 12:11
Jeff Storey
57.4k75 gold badges245 silver badges415 bronze badges
5 Comments
Sakthivel
Hi dude, I am still facing the same problem. Any idea?
Jeff Storey
What is the problem? Is the function being called? What are the values of x, y and z?
Sakthivel
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.
Jeff Storey
I mean what behavior are you actually seeing? What apart of it is not working? Are you even getting into the function call?
Sakthivel
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.
Try to place the script tag before the form tag, so the x,y and z will be declared before.
Comments
lang-js