Solved Question Bank of CSS 2
Solved Question Bank of CSS 2
**Usage**:
- **`break`**: Exits the nearest loop (`for`, `while`, `do-while`) or `switch`
statement.
- **`continue`**: Skips the rest of the current loop iteration and proceeds to
the next iteration.
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Break and Continue Example</title>
</head>
<body>
<script>
document.write("Using break:<br>");
for (let i = 1; i <= 5; i++) {
if (i === 3) break; // Exit loop when i is 3
document.write(i + " ");
}
document.write("<br>Using continue:<br>");
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // Skip the rest of this iteration when i is 3
document.write(i + " ");
}
</script>
</body>
</html>
```
2. **Client-Side vs. Server-Side Scripting**
**Client-Side Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Client-Side Scripting Example</title>
</head>
<body>
<button onclick="document.write('Hello from client-side script!')">Click
Me</button>
</body>
</html>
```
```php
<!-- hello.php -->
<!DOCTYPE html>
<html>
<head>
<title>Server-Side Scripting Example</title>
</head>
<body>
<?php
echo "Hello from server-side script!";
?>
</body>
</html>
3. **Function Definition Expression**
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Function Expression Example</title>
</head>
<body>
<script>
const greet = function() {
document.write("Hello from a function expression!");
};
greet();
</script>
</body>
</html>
```
---
4. **Switch Case**
The `switch` statement executes different code blocks based on the value of
a variable.
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Switch Case Example</title>
</head>
<body>
<script>
let dayNumber = 2;
switch (dayNumber) {
case 1:
document.write("Monday<br>");
break;
case 2:
document.write("Tuesday<br>");
break;
default:
document.write("Invalid day number<br>");
}
</script>
</body>
</html>
```
---
5. **Object in JavaScript**
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Object Example</title>
</head>
<body>
<script>
const person = {
firstName: 'John',
lastName: 'Doe',
greet() {
return `Hello, ${this.firstName} ${this.lastName}`;
}
};
document.write(person.greet());
</script>
</body>
</html>
```
---
Getters and setters allow custom behavior when getting or setting property
values.
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Getters and Setters Example</title>
</head>
<body>
<script>
const person = {
firstName: '',
lastName: '',
get fullName() {
return `${this.firstName} ${this.lastName}`;
},
set fullName(name) {
const parts = name.split(' ');
this.firstName = parts[0] || '';
this.lastName = parts[1] || '';
}
};
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Select Element Example</title>
</head>
<body>
<form>
<label for="fruit">Choose a fruit:</label>
<select id="fruit" name="fruit">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
<button type="button" onclick="showSelectedFruit()">Show Selected
Fruit</button>
</form>
<script>
function showSelectedFruit() {
const fruit = document.getElementById('fruit').value;
document.write('Selected fruit: ' + fruit);
}
</script>
</body>
</html>
```
---
8. **String Methods**
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>String Methods Example</title>
</head>
<body>
<script>
const str = 'Hello';
document.write("Unicode value of 'H': " + str.charCodeAt(0) + "<br>");
document.write("Character from Unicode 72: " +
String.fromCharCode(72));
</script>
</body>
</html>
```
---
9. **`splice()` Method**
**Syntax**:
```javascript
array.splice(start, deleteCount, item1, item2, ...);
```
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Splice Method Example</title>
</head>
<body>
<script>
let arr = [1, 2, 3, 4, 5];
arr.splice(2, 1, 6, 7); // Starts at index 2, removes 1 item, adds 6 and 7
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Character to Unicode Example</title>
</head>
<body>
<script>
const char = 'A';
document.write("Unicode of '" + char + "': " + char.charCodeAt(0));
</script>
</body>
</html>
```
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Armstrong Numbers Example</title>
</head>
<body>
<script>
for (let num = 1; num <= 100; num++) {
let sum = 0;
let temp = num;
while (temp > 0) {
let digit = temp % 10;
sum += digit * digit * digit;
temp = Math.floor(temp / 10);
}
if (sum === num) {
document.write(num + " is an Armstrong number<br>");
}
}
</script>
</body>
</html>
```
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Greatest Number Example</title>
</head>
<body>
<script>
const num1 = 5;
const num2 = 8;
const num3 = 3;
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Number Example</title>
</head>
<body>
<script>
function isPalindrome(num) {
const str = num.toString();
const reversed = str.split('').reverse().join('');
return str === reversed;
}
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>If Statement Example</title>
</head>
<body>
<script>
const age = 18;
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Concat vs Join Example</title>
</head>
<body>
<script>
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const combined = array1.concat(array2); // Merges arrays
document.write("Concatenated array: " + combined + "<br>");
---
---
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Student Sorting Example</title>
</head>
<body>
<script>
const students = [
{ name: 'Amit', marks: 70 },
{ name: 'Sumit', marks: 78 },
{ name: 'Abhishek', marks: 71 }
];
// Calculate average
const totalMarks = students.reduce((sum, student) => sum +
student.marks, 0);
const average = totalMarks / students.length;
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Check Uppercase Example</title>
</head>
<body>
<script>
function isFirstCharUppercase(str) {
return str.charAt(0) === str.charAt(0).toUpperCase();
}
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Merge Arrays Example</title>
</head>
<body>
<script>
function mergeAndRemoveDuplicates(arr1, arr2) {
const merged = arr1.concat(arr2);
return [...new Set(merged)];
}
---
21. **String Definition**
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>String Definition Example</title>
</head>
<body>
<script>
const str = "Hello, World!";
document.write("String: " + str);
</script>
</body>
</html>
```
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>String Functions Example</title>
</head>
<body>
<script>
const str = "Hello, World!";
document.write("Uppercase: " + str.toUpperCase() + "<br>");
document.write("Substring (0-5): " + str.substring(0, 5));
</script>
</body>
</html>
```
---
**Example**:
```html
<!DOCTYPE html>
<html>
<head>
<title>Array Example</title>
</head>
<body>
<script>
let arr = [1, 2, 3, 4, 5];
arr.splice(2, 0, 10); // Insert 10 at index 2