CSS_QB
CSS_QB
2. Host Objects: are objects that are supplied to JavaScript by the browser environment.
Example: Window
3. User-Defined Objects: are those that are defined by you, the programmer.
Example:
var person =
{
firstname: "Girija",
lastname: "Jore",
age: 10
};
3. <html>
4. <body>
5. <input type="text" name="" id="" onkeypress="alert('Key
pressed')">
6. <input type="text" name="" id="" onkeydown="alert('Key
down')">
7. <input type="text" name="" id="" onkeyup="alert('Key
released')">
8. <script>
9.
10. </script>
11. </body>
12. </html>
<body>
<div id="i1" style="border: 2px solid black;width:300px;height:
300px;" onmousemove="mouseMove(event)">
<p>onmousemove event
</p>
<p id="coords"></p>
</div>
<label for="">onmousedown</label>
<input type="text" onmousedown="alert('you pressed down mouse key')">
<script>
let container = document.getElementById("i1")
function mouseMove(e) {
document.getElementById("coords").innerText = `x: $
{e.clientX}, y: ${e.clientY}`
}
</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 :” + result);
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>
6. Define array with example.
Answer:
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"];
An array is a type of object used for storing multiple values in single
variable.
Each value (also called an element) in an array has a numeric position,
known as its index, and it may contain data of any data type-numbers,
strings, Booleans, functions, objects, and even other arrays.
The array index starts from 0, so that the first array element is arr [0].
The simplest way to create an array is by specifying the array
elements as a comma-separated list enclosed by square brackets, as
shown in the example below:
var cities = ["London", "Paris", "New York"];
alert(cities[2]); // Output: New York
var a = ["London", 500, ”aa56”, 5.6];
Answer:
ii. Build forms that respond to user input without accessing a server.
iii. Display clocks
iv. Client server validation
v. Dynamic drop-down menu
vi. Displaying date and time
vii. Opening and closing new window or frame
viii. Web applications
Answer:
9. Define Event. List 3 types of keybord events.
Answer:
The change in the state of an object is known as an Event.
4 types of event are:
1. onkeypress
2. onkeydown
3. onkeyup
Answer:
A function is called by using the function name followed by parentheses. If the function has
arguments, values for each argument are placed within the parentheses. The sequence of arguments
should be same as listed in the function definition.A comma must separate each value.
<html>
<body>
<script>
function add(num1, num2)
{
var sum =
num1 + num2
alert("Sum is " + sum)
}
var num1 = parseInt(prompt('Enter num 1’))
var num2 = parseInt(prompt('Enter num 2’))
add(num1, num2)
</script>
</body>
</html>
Answer:
Eg:
<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>
c) Number():converts a string into number.
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>
12. 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>
14. Write a JavaScript program to find the sum of squares of a numeric vector.
Answer:
<!DOCTYPE html>
<html>
<body>
<script>
function square(array) {
var sum = 0,
i = array.length;
while (i)
sum += Math.pow(array[i], 2);
return sum;
i--
}
document.write(square([9,5,7,8]));
</script>
</body>
</html>
15. 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],
let s = 0,
let p = 1,
let i;
for (i = 0; i < array.length; i += 1)
{
s = s+ array[i];
p = p* array[i];
}
document.write('Sum : '+s + ' Product : ' +p);
</script>
</body>
</html>
16. 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);
Syntax:
var stringname="string value";
var name = “Rohan”
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()" />
Answer:
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.