CSS_QB (1)
CSS_QB (1)
<html>
<body>
<input type="text" onkeydown="alert('You have pressed a key inside text input!')">
}
</script>
</body>
</html>
<html>
<head>
<style>
div {
width: 200px;
height: 100px;
border: 1px solid black;
}
</style>
</head>
<body>
<p>This example demonstrates how to assign an "onmousemove" event to a div element.</p>
<div onmousemove="myFunction(event)"></div>
<p>Mouse over the rectangle above, and get the coordinates of your mouse pointer.</p>
<p id="demo"></p>
<script>
function myFunction(e)
{
var x = e.clientX;
var y = e.clientY;
var coor = "Coordinates: (" + x + "," + y + ")";
document.getElementById("demo").innerHTML = coor;
}
</script>
</body>
</html>
<html>
<head>
<style>
div{ height:200px;
width:100px;
border:1px solid black;
}
</style>
<body>
<div id="demo" onmousemove="mouseDown()">
<script>
function mouseDown() {
document.getElementById("demo").style.backgroundColor="red";
}
</script>
</body>
</html>
4. Write a JavaScript code to perform all arithmetic operations using switch case.
Answer:
<html>
<body>
<script>
let x, y, result, ch;
ch = parseInt(prompt(“Enter your choice:”));
switch (ch) {
case 1:
result = x + y;
document.write(“\nResult is :” + result;)
break
case 2:
result = x – y;
document.write(“\nResult is :” + result);
break
case 3:
result = x * y;
document.write(“\nResult is :” + res);
break
case 4:
res = x / y;
document.write(“\nResult is :” + result);
break
case 5:
result = x % y;
document.write(“\nResult is :” + result);
break
default:
document.write(“Invalid Choice:” + ch);
}
</script>
</body>
</html>
5. Write a JavaScript code to find whether entered year leap year or not.
Answer:
<html>
<head>
<script>
function check_leapyear(){
var year;
year = document.getElementById("year").value;
</body>
</html>
Array is used to store a set of values (different types) in a single variable name. Arrays are the
fundamentals part of the most programming languages and scripting languages. Using array, we
can store multiple values under a single name.
Syntax: var array_name[item1 , item2]
var cities = ["London", "Paris", "New York"];
Answer:
i. Build forms that respond to user input without accessing a server.
ii. Display clocks
iii. Client server validation
iv. Dynamite drop-down menu
v. Displaying date and time
vi. Opening and closing new window or frame
vii. Build forms that respond to user input.
8. Write 2 ways to declare an array with example.
Answer:
a) By using new Array() constructor:
Call it with no arguments: var a= new Array();
This method is creates an empty array with no elements and is
equivalent to the array literal [].
Call it with a single numeric argument, which specifies a length:
Var a = new Array(10);
This technique creates an array with specified length.
Explicitly specify two or more array elements or a single non-numeric
element for the element for the array: Var a = new
Array(5,4,3,2,1,”testing”, “testing”);
In this form, the constructor arguments become the elements of the new
array
When we declare array using square brackets is called the “array literal
notation”:
Var x= [];
Var x= [5];
Answer:
10.Define Event. List 4 types of Mouse events.
Answer:
The change in the state of an object is known as an Event. There are various events in HTML
which represents that some activity is performed by the user or by the browser. When js code is
included in html it reacts over these events and allows the execution. This process of reacting is
known as event handling. Thus, js handles html events via event handler
i. onmouseout event
ii. onmouseover event
iii. onwheel event
iv. onmouseup event
v. onmousedown event
vi. ondblclick event
<html>
<body>
<script>
function IncreaseSalary(OldSalary, PercIncrease)
{
var NewSalary =
OldSalary * (1 + (PercIncrease / 100))
alert("Your new salary is " + NewSalary)
}
var Salary = prompt('Enter old salary.', ' ')
var Increase =
prompt('Enter salary increase as percent.', ' ')
IncreaseSalary(Salary, Increase)
</script>
</body>
</html>
Answer:
a) substr(): It is used to fetch the part of the given string on the basis of the specified
starting position and length.
<html>
<body>
<script type="text/javascript">
var str = "Javascript";
document.write(str.substr(0,6));
</script>
</body>
</html>
b) parseFloat( ): The parseFloat() method is used similarly to the parseInt() method, except
the parseFloat() method is used with any number that has a decimal value.
Example:
<html>
<body>
<script type="text/javascript">
var price = '10.95';
var converted = parseFloat(price);
document.write(converted);
</script>
</body>
</html>
Example:
<html>
<head>
<title>Number() Function </title>
</head>
<body>
<script type="text/javascript">
var a = '49';
var b = '1';
var c = Number(a) + Number(b);
document.write(c);
</script>
</body>
</html>
Example:
<html>
<head>
<title>NUmber() Function </title>
</head>
<body>
<script type="text/javascript">
var a = "49";
var digit = parseInt(a);
document.write(digit);
</script>
</body>
</html>
<html>
<body>
<script>
document.write(str.substr(0,6));
document.write(parseFloat(str1));
document.write(Number(str2)+Number(str3));
document.write(num)
</script>
</body>
</html>
13.Write a simple JavaScript program to join all elements of the following array into a string.
Sample array : myColor = ["Red", "Green", "White", "Black"];
Expected Output :
"Red,Green,White,Black"
"Red,Green,White,Black"
"Red+Green+White+Black"
Answer:
<html>
<body>
<script>
var color = ["Red", "Green", "White", "Black"];
var text = color.join();
var text1=color.join(";");
var text2=color.join("+");
document.write(text +"</br>");
document.write(text1 +"</br>");
document.write(text2);
</script>
</body>
</html>
</body>
</html>
document.write(square([9,5,7,8]));
</script>
</body>
</html>
16.Write a JavaScript program to compute the sum and product of an array of integers.
Answer:
<!DOCTYPE html>
<html>
<body>
<script>
var array = [4,5,8,9,7],
s = 0,
p = 1,
i;
for (i = 0; i < array.length; i += 1)
{
s += array[i];
p *= array[i];
}
document.write('Sum : '+s + ' Product : ' +p);
</script>
</body>
</html>
17.Define objects and write different ways to create an Object with example.
Answer:
Objects are collection of properties and methods.
A Methods is a function that is a member of an object.
A Property is a value or set of values that is the member of an object.
i. Define and create a single object, using an object literal.
Using an object literal, you both define and create an object in one statement.
Example:
var person = {
firstName: “Hhh",
lastName: “Bbb",
age: 10
};
ii. Define and create a single object, with the keyword “new” OR by creating instance of
Object
new keyword is used to create object.
Syntax: var objectname=new Object();
Example:
var person = new Object();
person.firstName = “Hhh";
person.age = 10;
iii. Define an object constructor, and then create objects of the constructed type.
Example:
function person(firstName, lastName, age)
{
this. firstName = firstName;
this. lastName = lastName;
this. age = age;
}
p=new person(“aaa”,”vvv”,10);
document.write(p.firstName+" "+p.lastName+" "+p.age);
18.How to declare string in JavaScript? Explain its with example.
Answer:
A) By string literal:
Syntax:
var stringname="string value";
var name = “Rohan”
Syntax:
var stringname=new String("string literal");
Example:
var name =new String("Rohan ");
alert("konnichiwa");
}
function India()
{
alert("namaste");
}
function Germany()
{
alert("Guten Tag");
}
</script>
</head>
<body>
<p>Hello in Different Countries</p>
<form>
<input type="button" value="Japan" onclick ="Japan()" />
i. IT is important to validate the form submitted by the user because it can have inappropriate
values.
ii. So, validation is must to authenticate user.
iii. JavaScript provides facility to validate the form on client server side validation.
iv. Through, JavaScript we can validate
Name
Password
Email
Date
Mobile number n many more
a) charCodeAt(): It provides the Unicode value of a character present at the specified index.
Var Unicode = StringName.charCodeAt()