Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ function and function call

Posted by: rjhmille May 22 2017, 02:18 PM

I'm trying to make the header a function and call it below. but am having a hard time:

<!DOCTYPE html>
<html>
<head><!-- set up the head of the page-->
<meta charset="UTF-8" >
<title>Bar Chart</title> <!-- Give the page a title -->

</head> <!-- close the page head -->
<body> <!-- Open the page body -->
<script> <!-- design the loop in java script -->
{
<!--Ask the user for the hight of their chart -->
var upperLimit = parseInt(prompt("How high is your chart?", ""));
var symBol = prompt("What symbol do you want to use?","");

//document.getElementById("num").value = upperLimit;
//document.getElementById("symbol").value = symBol;

<!-- set the upper limit of the counter to the user input -->}
}
for(var counter = 1; counter <= upperLimit; counter++)

{

document.write("<p>"+symBol+" </p>");
<!-- output the chart to the height the user entered -->
}

</script><!-- close the java script -->

</body> <!-- close the body -->

</html>

Posted by: pandy May 22 2017, 04:08 PM

Several things. The script isn't in HEAD. It's in BODY. There is no function and no function call either. But that doesn't matter. The below does.

You can't use HTML comments inside a script. You must use JS comments. // in front of a single line and /* and */ around a block.

You have curly braces where there shouldn't be any.

If you remove all of the above it boils down to the below and it runs.

CODE
var upperLimit = parseInt(prompt("How high is your chart?", ""));
var symBol = prompt("What symbol do you want to use?","");

//document.getElementById("num").value = upperLimit;
//document.getElementById("symbol").value = symBol;


for(var counter = 1; counter <= upperLimit; counter++)
{
   document.write("<p>"+symBol+" </p>");
}



Are you aware that document.write() will replace everything on the page?


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)