Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Q1.

write a javascript program that greet user according to the current timing

<html>
<body>

<script type="text/javascript">
var d = new Date();
var time = d.getHours();

if (time < 12)


{
document.write("<b>Good morning!</b>");
}
if (time > 12)
{
document.write("<b>Good afternoon!</b>");
}
if (time == 12)
{
document.write("<b>Go eat lunch!</b>");
}
</script>

</body>
</html>
Q2.Describe the input tag and write the form element used in javascript?

Ans: The <input> tag specifies an input field where the user can enter data. <input> elements are used
within a <form> element to declare input controls that allow users to input data.

Form element used in java script

Name DECRIPTION

<form> Defines an HTML form for user input


<input> Defines an input control
<textarea> Defines a multiline input control (text area)
<label> Defines a label for an <input> element
<fieldset> Groups related elements in a form
<legend> Defines a caption for a <fieldset> element
<select> Defines a drop-down list
<optgroup> Defines a group of related options in a drop-down list
<option> Defines an option in a drop-down list
<button> Defines a clickable button
<datalist> Specifies a list of pre-defined options for input controls
<output> Defines the result of a calculation
Q3. Explain alert method in a javascript with sutiable example?

ANS: An alert box is often used if you want to make sure information comes through to the user. The
alert box takes the focus away from the current window, and forces the browser to read the message

Ex: Window.alert("Hello! world");// The alert() method displays an alert box with a

specified message and an OK button.

Q4.write a program to set the html document background color in javascript?

Ans.

<html>

<body>

<script >

document.body.bgColor="blue ";

</script>

</body>

</html>

Q5. Define the join mehod and explain with the example?

The join() method joins the elements of an array into a string, and returns the string.

The elements will be separated by a specified separator. The default separator is comma (,).

Ex:-

var fruits = ["Banana", "Orange", "Apple", "Mango"]; // array having names of fruit.
var energy = fruits.join(); // join method.

Banana,Orange,Apple,Mango // output.
Q6.form validation in javascript?

<html>

<body>

<p> id="p1"</p>

<script>

function validateForm() {

var username="saurav";

var password="ssb007"

var x = document.forms["name"].value;

var y= document.forms["pass"].value;

if (x == " "&& y==" ") {

alert("Name must be filled out");

return false;

else

if(x!=username||y!=password)

document.getElementById("p1").innerHTML="enter the valid username and password";

else

document.write("welcome user");//or console.log("welcome user");

</script></body></html>
Q7 Write a javascript code to zoom-in and zoom-out the image?

<script type="text/javascript">

function zoomin(){ // called from html page onClick action.

var myImg = document.getElementById("sky");

var currWidth = myImg.clientWidth; // check the current width of image.

if(currWidth == 500){ // if current width is 500 ,then zoom-in reached.

alert("Maximum zoom-in level reached.");

} else{

myImg.style.width = (currWidth + 50) + "px"; // increase the width of image by 50 px each time

function zoomout(){ // called from html page onClick action.

var myImg = document.getElementById("sky");

var currWidth = myImg.clientWidth; // check the current width of image.


if(currWidth == 50){ // // if current width is 50 ,then zoom-out reached

alert("Maximum zoom-out level reached.");

} else{

myImg.style.width = (currWidth - 50) + "px"; increase the width of image by 50 px each time

</script>
Q7. What is various type of datatype in javascript.

Ans:

@ Primitive data-type:

1.Boolean.

2.NULL.

3.Undefined.

4.Number Var length=16.

5.String. Var lastName=Johnson

@ object. Var x={firstName=john,lastName=Doe};

Q What are the different type of operators?


Q what are the different type of control structure?

Ans.

1. If and if..else
2. For
3. Switch
4. With:

with( Math ) {
x = round( LN2 + E + pow( y, 4 ) );
}
Note that the 'with' statement brings extra variable names into the
current scope. In the example above, if I already had a variable called
pow before the 'with' statement, this variable(y) would be unavailable
inside the with statement, as it would have been replaced by the method of
the Math object (as would any other variables that matched property or
method names). Once the 'with' statement is complete, the old variables
would become available again.

5. While and do..while

You might also like