Javascript Oct 2022 in Sem Solved
Javascript Oct 2022 in Sem Solved
Q1) b) Explain different script tags that can be used in JavaScript. [5]
Answer:
In HTML, the `<script>` tag is used to include JavaScript. Different ways to use the `<script>`
tag include:
1. **Inline Script**: JavaScript code written directly within the `<script>` tags.
```html
<script>
alert("Hello, World!");
</script>
```
2. **External Script**: JavaScript code included from an external file.
```html
<script src="script.js"></script>
```
3. **Async Attribute**: Loads the script asynchronously without blocking the HTML parsing.
```html
<script src="script.js" async></script>
```
4. **Defer Attribute**: Loads the script after the HTML parsing is complete.
```html
<script src="script.js" defer></script>
```
Q2) a) Justify & explain how to add JavaScript code in an XHTML Document. [5]
Answer:
JavaScript can be added to an XHTML document using the `<script>` tag within the XHTML
structure. XHTML requires strict adherence to XML syntax rules. Example:
```xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML with JavaScript</title>
<script type="text/javascript">
function showAlert() {
alert("Hello, XHTML!");
}
</script>
</head>
<body>
<button onclick="showAlert()">Click Me</button>
</body>
</html>
```
XHTML documents must be well-formed, meaning all tags are properly closed, and attribute
values are quoted.
Q2) b) Elaborate Concept of Linked Scripts with an example. [5]
Answer:
Linked scripts refer to including external JavaScript files in an HTML document. This practice
promotes code reusability and separation of concerns. Example:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Linked Scripts Example</title>
<script src="script.js"></script>
</head>
<body>
<button onclick="showAlert()">Click Me</button>
</body>
</html>
```
`script.js`:
```javascript
function showAlert() {
alert("Hello from an external file!");
}
Q2) c) Analyze different flow control statements in JavaScript (JS). Explain any one with
an example. [5]
Answer:
JavaScript has several flow control statements, including:
1. **if-else**: Conditional statements to execute code blocks based on conditions.
2. **switch**: Evaluates an expression and executes code blocks based on matching case values.
3. **for**: Loop that iterates a specified number of times.
4. **while**: Loop that continues as long as a specified condition is true.
5. **do-while**: Loop that executes at least once before checking the condition.
6. **break**: Exits a loop or switch statement.
7. **continue**: Skips the current iteration of a loop and proceeds to the next iteration.
**Example: if-else**
```javascript
let num = 10;
if (num > 0) {
console.log("Positive number");
} else if (num < 0) {
console.log("Negative number");
} else {
console.log("Zero");
}
Q3) a) Write a JavaScript program to extract the values at the specified indexes from a
specified array. [5]
Answer:
```javascript
function extractValues(arr, indexes) {
let result = [];
for (let i = 0; i < indexes.length; i++) {
result.push(arr[indexes[i]]);
}
return result;
}
let array = [10, 20, 30, 40, 50];
let indexes = [1, 3, 4];
let values = extractValues(array, indexes);
console.log(values); // Output: [20, 40, 50]
Q4) b) Create an object in JavaScript and add different properties to that object
dynamically. [5]
Answer:
```javascript
let person = {}; // Create an empty object
person.name = "John"; // Add property dynamically
person.age = 30;
person.sayHello = function() {
console.log("Hello, my name is " + this.name);
};
Q4) c) What is the typeof operator in JavaScript? List out values returned by typeof
operator. [5]
Answer:
The `typeof` operator in JavaScript returns a string indicating the type of the operand. Values
returned by `typeof` include:
1. `"undefined"`:
**Example:**
```javascript
console.log(typeof undefined); // "undefined"
console.log(typeof true); // "boolean"
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof null); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof Symbol()); // "symbol"
console.log(typeof 10n); // "bigint"