Sakthivel
Sakthivel

Reputation: 666

JavaScript calling function is not working

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>

Upvotes: 0

Views: 73

Answers (3)

Jeff Storey
Jeff Storey

Reputation: 57182

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

Upvotes: 2

lcestari
lcestari

Reputation: 178

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

Upvotes: 0

TheMobileMushroom
TheMobileMushroom

Reputation: 126

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==''){}

Upvotes: 3

Related Questions