Css 4-6
Css 4-6
Arithmetic Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
++ (Increment)
-- (Decrement)
Logical Operators:
|| (Logical OR)
! (Logical NOT)
Dynamic Typing: JavaScript allows variables to hold different data types at different times.
Interpreted Language: JavaScript is interpreted, meaning it is executed directly in the browser without prior
compilation.
Event Handling: JavaScript can respond to user actions like clicks, keypresses, etc.
Cross-Browser Compatibility: JavaScript runs on all modern web browsers without requiring additional plugins.
Example:
var person = {
name: "John",
age: 25
};
prompt():
alert():
Displays a message box.
Example: alert("Hello!");
Example:
var person = {
firstName: "John",
lastName: "Doe",
get fullName() {
},
set fullName(name) {
this.firstName = parts[0];
this.lastName = parts[1];
};
if (i % 2 === 0) {
document.write(i + "<br>");
7. Explanation of Terms
var i = 1;
i++;
9. Explanation of Tokens
Syntax:
prompt(message, defaultValue);
Explanation: prompt() displays a dialog box that prompts the user for input. message is the text displayed to the
user, and defaultValue is optional.
arr.reverse();
Syntax:
with (object) {
// Statements
Use: Simplifies code by allowing you to reference an object’s properties without repeatedly specifying the
object name.
if (num === 1) {
isPrime = false;
} else {
if (num % i === 0) {
isPrime = false;
break;
Example:
Example:
<script>
alert("Hello, World!");
</script>
Example: "Hello".indexOf("e") → 1
Example:
In JavaScript, an object can be used as an associative array (also known as a dictionary). The keys are strings,
and they are used to access values associated with them. Here's an example:
```javascript
let student = {
name: "John",
age: 20,
grade: "A"
};
```
In this example, `student` is an object with keys `name`, `age`, and `grade`, each associated with a specific
value.
---
```javascript
function countVowels(str) {
let count = 0;
if (vowels.includes(char)) {
count++;
return count;
}
console.log(countVowels("Hello World")); // Output: 3
```
This function iterates through the string, checks each character against a list of vowels, and increments the
count accordingly.
---
|----------------------------|------------------------------------|-----------------------------------|
| **Purpose** | Combines two or more arrays | Joins all array elements into a string |
| **Original Array** | Does not modify the original array | Does not modify the original array|
| **Combines Arrays** | Yes, it combines multiple arrays | No, just joins elements of one array |
---
```javascript
console.log(person["age"]); // Output: 25
```
- **Setting Properties**: You can add new properties or modify existing ones by assigning values.
```javascript
console.log(person);
```
- **Deleting Properties**: Use the `delete` keyword to remove a property from an object.
```javascript
console.log(person);
```
---
```javascript
function isArmstrong(num) {
let sum = 0;
function generateArmstrongNumbers() {
if (isArmstrong(i)) {
console.log(i);
```
This script checks each number within the range 1 to 100 to see if it's an Armstrong number by summing the
cubes of its digits and comparing the result to the original number
1. Write a JavaScript program which compute, the average marks of the following students
Then, this average is used to determine the corresponding grade.
function calculateAverage(arr) {
let total = 0;
total += arr[i];
}
// Function to determine grade based on average marks
function determineGrade(average) {
return "F";
return "D";
return "C";
return "B";
} else {
return "A";
Here are simple, easy-to-understand JavaScript solutions for each of the tasks:
---
```javascript
---
### 8. Input a String from the User and Calculate Its Length
This prompts the user to enter a string and then calculates its length.
```javascript
```
---
```javascript
function isPalindrome(str) {
if (isPalindrome(word)) {
} else {
```
---
### 10. Convert Character to Unicode and Vice Versa, Display Data Type
This converts a character to its Unicode value and vice versa, and also shows its data type.
```javascript
```
---
```javascript
```
---
### 12. Find and Display the Number of Duplicate Values in an Array
```javascript
arr.forEach((item) => {
duplicates[item] = (duplicates[item] || 0) + 1;
});
for (let key in duplicates) {
if (duplicates[key] > 1) {
```
---
```javascript
function countVowels(str) {
let count = 0;
if (vowels.includes(char)) {
count++;
return count;
```
---
This takes 10 numbers, adds a new one, sorts the array, and displays the result.
```javascript
numbers.push(num);
numbers.push(newNum);
```
---
```javascript
let person = {
firstName: "John",
lastName: "Doe",
get fullName() {
},
set fullName(name) {
this.firstName = parts[0];
this.lastName = parts[1];
};
```
---
### 16. Classify Age Group Using Nested Switch-Case
```javascript
switch (true) {
console.log("Child");
break;
console.log("Teenager");
break;
switch (true) {
console.log("Young Adult");
break;
console.log("Middle-Aged Adult");
break;
break;
console.log("Senior");
break;
default:
console.log("Invalid age.");
```
---
```javascript
let employees = [
];
```
---
```javascript
function capitalizeWords(sentence) {
console.log(capitalizeWords(sentence));
```
---
```javascript
```
---
```javascript
arr.shift();
arr.unshift(0);
arr.pop();
arr.push(5);
```
These JavaScript snippets are simple, easy to understand, and demonstrate how to work with different functions
and features in JavaScript.