Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPrakhar Patwa
11,260 Pointsi have a question regarding function
my js code is
function getArea(length , width)
{
var area = length * width ;
return "<h1>Area of <a href='#' style = 'color:red; text-decoration:none;' >Rectangle</a> is " + area + "</h1>";
}
document.write( getArea(34 , 22) );
=============================================
how to take a user input in this?
prompt("what is the length?");
prompt("what is the width?");
function returnValue(a){
return a ;
}
var echo = returnValue('My argument');
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsYou could either assign the return value of the prompt
function calls to two variables as Vitor Freitas recommended, or you could just call the getArea
function, passing in the prompt
function calls as parameters:
function myFunc(x, y) {
// function code here...
}
var x = prompt("What is x?");
var y = prompt("What is y?");
myFunc(x, y);
OR
function myFunc(x, y) {
// function code here...
}
myFunc(prompt("What is x?"), prompt("What is y?"));
Obviously the first is a bit easier to read and understand.
Vitor Freitas
3,579 PointsYou can do it this way :
var length= prompt("what is the length?"),
width = prompt("what is the width?");
Prakhar Patwa
11,260 Pointswill it be in the function or in the argument?
Prakhar Patwa
11,260 PointsPrakhar Patwa
11,260 PointsOkay got it.
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsIf you feel that your question has been answered, please select an answer as the 'Best Answer' so that other Treehouse users know that this question has been resolved.