Important Questions and Model Answers for JavaScript (22519)
Unit I: Basics of JavaScript Programming
1. Explain any two features of JavaScript.
- Lightweight: Minimal syntax, suitable for client-side scripting.
- Event-driven: Can handle user actions dynamically.
2. Write a JavaScript to display a Welcome message.
```javascript
alert("Welcome to JavaScript Programming!");
```
3. Write a JavaScript program to check whether a number is even or odd.
```javascript
let num = 5;
if (num % 2 === 0) {
[Link]("Even");
} else {
[Link]("Odd");
}
```
4. Write a JavaScript to display squares of numbers from 1 to 10 using a loop.
```javascript
for (let i = 1; i <= 10; i++) {
[Link](i * i);
}
```
Unit II: Array, Function, and String
1. Define an array and explain how to declare it.
An array is a collection of elements stored at contiguous memory locations.
Declaration:
```javascript
let arr = [1, 2, 3, 4];
```
2. Write a JavaScript program to reverse a string.
```javascript
let str = "JavaScript";
let reversed = [Link]('').reverse().join('');
[Link](reversed); // Output: tpircSavaJ
```
3. Explain the use of `push()` and `pop()` methods in JavaScript.
- `push()`: Adds an element to the end of the array.
- `pop()`: Removes the last element from the array.
Example:
```javascript
let arr = [1, 2, 3];
[Link](4); // [1, 2, 3, 4]
[Link](); // [1, 2, 3]
```
Unit III: Form and Event Handling
1. What is an event handler? Give an example.
An event handler is a function that executes when a specific event occurs.
Example:
```javascript
[Link] = function() {
alert("Button clicked!");
};
```
2. Write a JavaScript program to validate a form with a name and email field.
```javascript
function validateForm() {
let name = [Link]("name").value;
let email = [Link]("email").value;
if (name === "" || email === "") {
alert("Name and Email cannot be empty.");
} else {
alert("Form submitted.");
}
}
```
Unit IV: Cookies and Browser Data
1. Write a JavaScript program to create, read, and delete a cookie.
```javascript
// Create a cookie
[Link] = "username=JohnDoe; expires=Fri, 31 Dec 2023 23:59:59 UTC;";
// Read the cookie
[Link]([Link]);
// Delete the cookie
[Link] = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
```
2. Explain the `[Link]()` method with an example.
The `[Link]()` method opens a new browser window.
Example:
```javascript
let newWindow = [Link]("[Link] "_blank",
"width=500,height=500");
```
Unit V: Regular Expression, Rollover, and Frames
1. Write a regular expression to validate an email address.
```javascript
let emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
[Link]([Link]("example@[Link]")); // true
```
2. Write a JavaScript program to implement a rollover effect for images.
```html
<img src="[Link]" > >```
Unit VI: Menus, Navigation, and Web Page Protection
1. Write a JavaScript program to create a floating menu.
```html
<div style="position:fixed; top:10px; left:10px;">Floating Menu</div>
```
2. Write a JavaScript program to disable right-click on a webpage.
```javascript
[Link]("contextmenu", function(e) {
[Link]();
alert("Right-click is disabled");
});
```