0% found this document useful (0 votes)
4 views12 pages

QBCSS Solve

The document contains various JavaScript programs and explanations, including checking if a number is a multiple of 3 or 7, displaying 'Hello World!', and defining objects and arrays. It also discusses operators, features of JavaScript, associative arrays, function syntax, string manipulation, and form handling in HTML. Additionally, it provides examples of counting vowels, finding duplicates in an array, and creating different types of input fields in forms.

Uploaded by

tanvi.digikraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

QBCSS Solve

The document contains various JavaScript programs and explanations, including checking if a number is a multiple of 3 or 7, displaying 'Hello World!', and defining objects and arrays. It also discusses operators, features of JavaScript, associative arrays, function syntax, string manipulation, and form handling in HTML. Additionally, it provides examples of counting vowels, finding duplicates in an array, and creating different types of input fields in forms.

Uploaded by

tanvi.digikraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. WAP to check if a given positive number is a multiple of 3 or 7.

<!DOCTYPE html>
<html>
<head>
<title>Multiple of 3 or 7</title>
</head>
<body>
<script>
function checkMultiple(num) {
if (num % 3 === 0 && num % 7 === 0) {
return num + " is a multiple of both 3 and 7.";
} else if (num % 3 === 0) {
return num + " is a multiple of 3.";
} else if (num % 7 === 0) {
return num + " is a multiple of 7.";
} else {
return num + " is not a multiple of 3 or 7.";
}
}

// Prompt the user for input


let number = prompt(parseInt("Enter a number:");
number = parseInt(number); // Convert the input to an integer
document.write(checkMultiple(number)); // Display the result on the page
</script>
</body>
</html>

2. WAP to display “Hello World!” in JS.


<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World Example</title>
</head>
<body>
<script>
document.write("<i>Hello World!</i>");
</script>
</body>
</html>
3. Explain what an object in JS is.
A JS object is a collection of properties and methods. It is a way to group data together and
store it in a structured way. Objects can be created with ‘{}’ with an optional list of properties.
-Syntax:
let obj_name={
key1:value1,
key2:value 2,

key n:value n
};
 obj_name – Each object should be uniquely identified by a name or IDs in webpage to
distinguish between them.
 property – The data in the object is stored in key-value pairs, called a property. The key
is a string while value can be any data-type, including another object.
-Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Object Example</title>
<script>
let bag = {
books: 3,
snacks: ['apple', 'chips'],
bottle: "full",
addBooks: function() {
this.books++;
}
};

// Call the method to add a book


bag.addBooks();

// Output the updated number of books


document.write("Number of books:", bag.books); // Should output 4
</script>
</head>
<body>
</body>
</html>

4. State the differences between ‘continue’ and ‘break statement’.


5. Enlist different types of operators. Explain any one type in detail.

In JavaScript, operators are symbols or keywords that perform operations on variables and
values. The different types of operators in JavaScript include:

1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. String Operators
7. Conditional (Ternary) Operator
8. Type Operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations on numbers. JavaScript


supports several arithmetic operators, including:

 Addition (+): Adds two numbers or concatenates two strings.


 Subtraction (-): Subtracts one number from another.
 Multiplication (*): Multiplies two numbers.
 Division (/): Divides one number by another.
 Modulus (%): Returns the remainder of a division.
 Exponentiation (**): Raises the first number to the power of the second number.
 Increment (++): Increases a number by one.
 Decrement (--): Decreases a number by one.
<!DOCTYPE html>

<head>

<title>Op</title>

<script>

let a = 10;

let b = 5;

let sum = a + b; // Addition: sum is 15

let difference = a - b; // Subtraction: difference is 5

let product = a * b; // Multiplication: product is 50

let quotient = a / b; // Division: quotient is 2

let remainder = a % b; // Modulus: remainder is 0

a++; // Increment: a becomes 11

b--; // Decrement: b becomes 4

document.write("Sum: " + sum + "<br>");

document.write("Difference: " + difference + "<br>");

document.write("Product: " + product + "<br>");

document.write("Quotient: " + quotient + "<br>");

document.write("Remainder: " + remainder + "<br>");

document.write("Value of a after increment: " + a + "<br>");

document.write("Value of b after decrement: " + b + "<br>");

</script></head><body></body></html>
6. Explain any four features of JS.
o Lightweight: JavaScript is designed to be easy to use and has a small footprint, making
it efficient for web development.
o Dynamic Typing: Variables in JavaScript do not need to declare a type. A variable can
hold different types of data at different times.
o Interpreted Language: JavaScript code is executed directly by the web browser without
the need for prior compilation.
o Event-Driven: JavaScript responds to events like user actions (clicks, key presses),
making it ideal for interactive web pages.
o Versatile: JavaScript can be used for both front-end (client-side) and back-end (server-
side) development.
o Object-Based: Although not purely object-oriented, JavaScript supports objects and can
create and manipulate them

7. Explain what an associative array is.


Associative Array:
An associative array is essentially an object in JS. They store a collection of key–value pairs.
Each key is unique & used to access the corresponding value. Unlike a regular array that uses
numeric indices, associative arrays use keys that are usually strings or other data types.
Object as an associative array:
In JS, there isn’t a native data type called an ‘associative array’, but objects serve similar
purposes. The properties of an object act as the key & the corresponding values can be any
valid JD data type. They do not have a ‘length’ property like a normal array & cannot be
traversed using normal for.

8. Write the syntax of function definition and function call.

Function Definition Syntax


function functionName(parameters) {
// Function body: code to be executed
Return value;
}

 functionName: The name you give to your function. It should be descriptive of what the
function does.
 parameters: Optional. These are input values that the function can receive when called. They
are separated by commas.
 Function body: The code inside the curly braces {} is executed when the function is called.

Example of a Function Definition:


function greet(name) {
console.log("Hello, " + name + "!");
}
Function Call Syntax
functionName(arguments);

 functionName: The name of the function you want to call.


 arguments: The values you pass to the function’s parameters. These are optional and should
match the number and order of the parameters in the function definition.

Example of a Function Call:


greet("Alice");

9. WAP to replace “like” by “know” and divide the string on the basis
of white spaces from the 7th index and display results. (Given string
= “I like JavaScript programming”)

<!DOCTYPE html>
<html>
<head>
<title>String Manipulation</title>
</head>
<body>
<script>
// Given string
let givenString = "I like JavaScript programming";

// Replace "like" with "know"


let modifiedString = givenString.replace("like", "know");

// Split the string at the 7th index


let part1 = modifiedString.slice(0, 7);
let part2 = modifiedString.slice(7);

// Display results
document.write('<p>Modified String: ' + modifiedString + '</p>');
document.write('<p>Part 1: ' + part1 + '</p>');
document.write('<p>Part 2: ' + part2 + '</p>');
</script>
</body>
</html>
10. WAP to count and display the number of vowels in a given string.

<!DOCTYPE html>

<html>

<head>

<title>Count Vowels</title>

</head>

<body>

<script>

function countVowels(str) {

// Define vowels

const vowels = 'aeiouAEIOU';

let count = 0;

// Count vowels

for (let char of str) {

if (vowels.includes(char)) {

count++;

return count;

// Given string

let givenString = "This is a simple example.";

// Count vowels

let vowelCount = countVowels(givenString);


// Display result

document.write('Number of Vowels: ' + vowelCount);

</script>

</body>

</html>

11. Explain what an array is and how to declare an array along with examples.
An array in JavaScript is a special data structure that allows you to store multiple values in a
single variable. These values can be of any type, including numbers, strings, objects, or even
other arrays. Arrays are ordered collections, meaning the values are stored in a specific
sequence, and each value can be accessed by its index, which starts at 0.

Key Characteristics of Arrays:

 Indexed: Each element in an array has a numeric index, starting from 0 for the first element.
 Dynamic: Arrays can grow or shrink in size; you can add or remove elements as needed.
 Mixed Types: Arrays can store elements of different types (e.g., numbers, strings, objects).

Ways to construct an array in JS:

1) JS array literal
Syntax: var array-name = [val1, val2...valn];
Example: var fruits = ["apple", "banana", "cherry"];
console.log(fruits); // Outputs: ["apple", "banana", "cherry"]
2) Creating instance of array directly
Syntax: var array_name =new Array ( ); // declaration
array_name[0] = val1;
array_name[1] = val2; //initialization
Example: var colors = new Array(); // Declare an empty array
colors[0] = "red"; // Initialize elements
colors[1] = "green";
colors[2] = "blue";
console.log(colors); // Outputs: ["red", "green", "blue"]
3) JS array constructor
Syntax: var array_name = new Array (val1, val2..., valn);
Example: var numbers = new Array(10, 20, 30, 40);
console.log(numbers); // Outputs: [10, 20, 30, 40]
12. WAP to find and display the duplicate values along with the count in an integer array.

<!DOCTYPE html>
<html>
<head>
<title>Find Duplicates</title>
</head>
<body>
<script>
function findDuplicates(arr) {
const counts = {};
const duplicates = {};

// Count occurrences of each number


for (const num of arr) {
counts[num] = (counts[num] || 0) + 1;
}

// Find duplicates
for (const num in counts) {
if (counts[num] > 1) {
duplicates[num] = counts[num];
}
}

return duplicates;
}

// Given array
const givenArray = [1, 2, 3, 2, 4, 5, 1, 6, 1];

// Find duplicates
const duplicateCounts = findDuplicates(givenArray);

// Display result
document.write('<p>Given Array: ' + givenArray + '</p>');
document.write('<p>Duplicates and Counts:</p>');
for (const [num, count] of Object.entries(duplicateCounts)) {
document.write('<p>Number ' + num + ' appears ' + count + ' times</p>');
}
</script>
</body>
</html>
13. What is a Form?

In JavaScript, a form typically refers to an HTML form element that allows users to
submit data to a web server. Forms are a fundamental part of web development, enabling
user interaction and data collection, such as filling out surveys, signing up for
newsletters, or submitting login credentials.

Key Components of an HTML Form:

 Form Element (<form>): The container that holds all form-related input elements.
 Input Elements: Various HTML elements like <input>, <textarea>, <select>, etc., where
users can enter data.
 Submit Button: A button that triggers the submission of the form data to the server.

14. How to create a password field in an html form?


To create a password field in an HTML form, you use the <input> element with the type
attribute set to "password". This ensures that the characters typed into the field are masked
(usually displayed as dots or asterisks) to hide the user's input for security reasons.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Password Field Example</title>
</head>
<body>
<form action="/submit-form" method="POST">
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

15. Explain any 4 attributes of the input tag with examples.

button Type

The button type creates a clickable button in a form. Unlike the submit button, which submits
the form data, a button can be used for any purpose, including triggering JavaScript actions.

Key Attributes:

 type="button": Specifies the button type.


 value: The text displayed on the button.
 onclick: JavaScript code to execute when the button is clicked.
Example:

<input type="button" value="Click Me" onclick="alert('Button Clicked!')">

The checkbox type creates a checkbox, allowing the user to select one or more options. Multiple
checkboxes can be used together to provide multiple selection options.

Key Attributes:

 type="checkbox": Specifies the checkbox type.


 name: The name of the input field, which is used when the form is submitted.
 value: The value that is submitted if the checkbox is checked.
 checked: Indicates that the checkbox should be checked by default.
 Example:

<label><input type="checkbox" name="subscribe" value="newsletter" checked>


Subscribe to newsletter</label><br>

<label><input type="checkbox" name="terms" value="agree"> Agree to terms and


conditions</label>

date Type

The date type creates an input field that allows the user to select a date from a date picker. The
selected date is typically in the format YYYY-MM-DD.

Key Attributes:

 type="date": Specifies the date picker type.


 name: The name of the input field.
 value: The default date value in YYYY-MM-DD format.
 min: The minimum date allowed.
 max: The maximum date allowed.

Example:

<label for="birthday">Select your birthday:</label>

<input type="date" id="birthday" name="birthday" value="2000-01-01"


min="1950-01-01" max="2023-12-31">
email Type

The email type creates an input field for email addresses. The browser can validate the input to
ensure it follows the standard email format (e.g., [email protected]).

Key Attributes:

 type="email": Specifies the input as an email field.


 name: The name of the input field.
 value: The default email address.
 placeholder: Provides a hint to the user about what to enter.
 required: Ensures the email field must be filled before form submission.
 Example:

<label for="email">Enter your email:</label>

<input type="email" id="email" name="email" placeholder="[email protected]" required>

file Type

The file type creates an input field that allows the user to upload files from their device. This is
often used for uploading documents, images, or other files to a server.

Key Attributes:

 type="file": Specifies the input as a file upload field.


 name: The name of the input field.
 accept: Specifies the types of files that can be uploaded (e.g., image/* for images, .pdf for
PDF files).
 multiple: Allows multiple files to be selected for upload.

Example:

<label for="resume">Upload your resume:</label>


<input type="file" id="resume" name="resume" accept=".pdf,.doc,.docx">

You might also like