user5726628
user5726628

Reputation:

JavaScript arguments, unexpected number error

When parsing the following argument the console.log states that the two numbers (arguments) are unexpected. Why is this and how could I fix this error?

function max(20, 20) {
 return width * height;
}
console.log(max(width, height));

Upvotes: 0

Views: 659

Answers (4)

Mathews Mathai
Mathews Mathai

Reputation: 1707

Note the changes:

function max(width, height) //pass 20 to both width and height by calling function this way->> max(20,20);.
       {
       return width * height; 
       } 
       console.log(max(width, height));  
      //printing width and height

Now,why is function max(20,20) wrong?

  • Whatever goes inside a function paranthesis(argument) should be a variable declaration(or name of a predefined variable),it can not be a value. You had entered 20,20 and 20 is not treated as a variable because variable names cannot begin with a number in javascript and almost all other languages.
  • That is the reason you are getting an error.

Well,I know that you are confused because you have seen stuff like max(20,20);. Haven't you? This is called calling the function and passing value to it. I guess this is what you wanted to do.

  • max(20,20); is a statement in which you are calling the function max. Here (20,20) is valid beacuse you can enter value as well because these values are supposed to be passed to width and height.

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You don't have to pass values as arguments in definition of function, but when you'll call it, so your code should be like example bellow.

Hope this helps.


//Definition
function max(width , height) {
  return width * height;
}

//Call
console.log(max(20, 20));

Upvotes: 0

Grigoris Dimopoulos
Grigoris Dimopoulos

Reputation: 124

When you define a function you tell the function the arguments you are going to use ,it is something like defining a variable to use it inside the function.You can't define number as arguments.Try to do it like this:

 function max(width , height) 
{
     return width * height;
}
console.log(max(20, 20));

and pass the numbers you want ,when you use the function ,not when you define it.

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

It should be this way (your arguments cannot be direct values, it should be variables. And while calling them you should use values)

function max(width, height) {
 return width * height; 
}
console.log(max(20,20));

Upvotes: 1

Related Questions