Saturday, 15 August 2015

CS101 Face to Face Session 10 to 13 August


                                                        FACE to FACE Session

TOPIC of Discussion
As we know there is no such data type defined in JavaScript. We have just “var” to store any kind of data i.e. digits, characters, string etc. So we can use “var” in variety of ways. Here we have to use “var” in two ways. In loops it is used as digit and in displaying the “*” it is used as character string.
“Document.write” is a function to display commonly for displaying any kind of output in JavaScript. “Document.write” replaces all contents of web page with output. For displaying your output on same webpage you have to use “innerHTML”. This assign any output to specific element by using “getElementById()”. 
For loop is for repeating any sequence for specific time. For loop gives output multiple times. For storing all these outputs we have to use “var” as string and at the end we have to assign this string any element to display.
As in this program we have a text box input of number of rows in pyramid. We use this input as limit of for loop. There are two loops. First for loop is for Rows in pyramid and second for loop is for columns (number of “*”) in each row.
When you put an input and press build button this program will draw a pyramid according to your input.  This pyramid is in shape of “*”.


                                                            _____The End_____

Solution:


<html><head><title>
Lab Work
</title>

<script>
function pyramid()
{
var text="";
var j= document.getElementById("value").value;

if(j==0)
{
alert("Please enter a valid value");
document.getElementById("display").innerHTML = "";
}

for(var i = 0; i <j; i++) 
{
for(var x = 1; x <= i; x++)
{
text = text + "*"  ;
}
text = text + "*" + "<br>";
document.getElementById("display").innerHTML = text;
}

}
</script>

</head>
<body>
<h1 align="center">Lab Work</h1>
<p align="center">Please Enter Number of Rows for Pyramid and press the button</p>
<center>
<input type="text" id="value"><br><br>
<button onclick="pyramid()">Built</button>
<p id="display"></p>
</center>

</body></html>

No comments:

Post a Comment