Ashley P.

asked • 11/19/19

Javascript functions

Write a JavaScript function to calculate difference of square values of two numbers passed as function arguements and to display the absolute value of it in an alert box.


function v(5,3){

var a =5 ;

var b =3;

var r =(a*a)-(b*b);

var s = Math.abs(r);

alert("Absolute value = "+s);


Why this is not working?

1 Expert Answer


Mel M. answered • 11/19/19

Senior Software Engineering Student

Ashley P.

Hi Mel, thanks a lot for the response! I want this function to work with any two numbers. How do I do that? Thanks!


Mel M.

Hi Ashley, that's a simple fix. Instead of forcing the function to only accept the numbers 5 and 3, change it two accept two variables like a and b. This way the function can work with any number. You don't need to define a or b inside of the function for this same reason. Once you are ready to run the function, you can call it with any two numbers you want. I edited your function so that it reflects my explanation above. function v(a,b){ var a ; var b ; var r =(a*a)-(b*b); var s = Math.abs(r); alert("Absolute value = "+s); } var(4,6) I hope that this answers your question.


Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.