0% found this document useful (0 votes)
31 views

WT Assignment - 1

JavaScript is a programming language used primarily for creating interactive front-end web applications and providing dynamic functionality to websites without page reloads. It runs in the user's browser and is used to create dynamic user interfaces, while server-side scripting runs on the web server and is used for complex processing tasks and security. JavaScript shares similarities with other scripting languages like dynamic typing and being interpreted, but is unique in its ability to modify web pages through the DOM and its primary use for client-side scripting in browsers.

Uploaded by

dhruvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

WT Assignment - 1

JavaScript is a programming language used primarily for creating interactive front-end web applications and providing dynamic functionality to websites without page reloads. It runs in the user's browser and is used to create dynamic user interfaces, while server-side scripting runs on the web server and is used for complex processing tasks and security. JavaScript shares similarities with other scripting languages like dynamic typing and being interpreted, but is unique in its ability to modify web pages through the DOM and its primary use for client-side scripting in browsers.

Uploaded by

dhruvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is JavaScript? What is its purpose?

 JavaScript is a programming language used primarily for creating interactive front-end web applications. Its purpose
is to provide dynamic functionality to websites, allowing them to respond to user interactions and events in real-time
without the need for page reloads. JavaScript is often used alongside HTML and CSS to create modern, responsive
and engaging web applications that can run on any device with a web browser. It is a popular and widely-used
language in web development, with a vast array of libraries and frameworks available to developers.

2. Compare and contrast Client Side and Server Side Scripting.


Client-side scripting Server-side scripting
 Runs in the user's browser Runs on the web server
 Uses programming languages like JavaScript, HTML, Uses programming languages like PHP, Ruby, and
and CSS Python
 Main purpose is to create dynamic user interfaces and Main purpose is to generate dynamic content and handle
add interactivity to web pages complex processing tasks
 Executes on the client's computer or device, so it can Executes on the server, so it can provide a more secure
reduce server load and improve website performance environment for processing sensitive information
 Can be viewed and manipulated by users, making it Cannot be viewed or manipulated by users, making it
less secure than server-side scripting more secure than client-side scripting
In summary, client-side scripting is mainly used to create interactive user interfaces and improve website
performance, while server-side scripting is used for handling complex processing tasks and ensuring security.

3. Compare and contrast JavaScript with other Scripting Languages.


 When compared to other scripting languages, here are some key similarities and differences:

 Similarities:
 Like other scripting languages, JavaScript is interpreted rather than compiled.
 It is dynamically typed, meaning variables can change type at runtime.
 It is a high-level language, which means that it is closer to human language and is easier to read and write than
lower-level languages like assembly.
 Differences:
 JavaScript is primarily used for client-side scripting within web browsers, while other scripting languages like
Python and Ruby are more commonly used for server-side scripting and general-purpose programming.
 JavaScript has a unique feature of being able to modify the Document Object Model (DOM) of a web page,
which allows developers to create interactive web pages and applications with ease.
 JavaScript has a C-like syntax, which may be more familiar to developers who are used to programming in
languages like C++ or Java. Other scripting languages like Python and Ruby have a more flexible syntax that
allows for more natural language constructs.
 JavaScript is often criticized for its inconsistent behavior across different web browsers, which can make it
difficult to write cross-browser compatible code.
 In summary, while JavaScript shares some similarities with other scripting languages, its unique features and
use cases set it apart from other languages in the scripting realm.

4. What are the advantages of JavaScript?


 JavaScript has several advantages, including:
 Client-side scripting: JavaScript runs on the client-side, which means it can be executed on the user's browser
without requiring server interaction. This makes web pages more responsive and reduces server load.
 Cross-platform compatibility: JavaScript code can be run on any platform or device that supports a web browser,
including desktops, mobile phones, and tablets.
 Interactivity: JavaScript allows for the creation of interactive web applications and user interfaces, enabling dynamic
user experiences.
 Large community: JavaScript has a large and active community of developers who contribute to open source
libraries and provide support and resources to fellow developers.
 Versatility: JavaScript can be used for both front-end and back-end development, allowing for full-stack web
development using a single programming language.
 Easy to learn: Compared to other programming languages, JavaScript has a relatively simple syntax and can be
learned quickly by new developers.
Integrates with other technologies: JavaScript can be integrated with other web technologies such as HTML and
CSS to create rich and dynamic web applications.

5. Explain basic syntax of JavaScript. How can one use JavaScript internal to HTML and externally?
 The basic syntax of JavaScript includes variables, functions, conditional statements, loops, and objects.
 To use JavaScript internal to HTML, you can include the code within a <script> tag within the HTML document.
For example:
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World!</h1>
<script>
let greeting = "Hello World!";
alert(greeting);
</script>
</body>
</html>

 To use JavaScript externally, you can save the code in a separate file with a .js extension and include it in the HTML
document using the <script> tag.
For example:
<html>
<head>
<title>My Page</title>
<script src="script.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

6. Explain variables, datatypes and operators used in JavaScript.


 Variables: A variable is a container that holds a value. In JavaScript, variables can be declared using the "var", "let",
or "const" keyword. The value of a variable can be changed or reassigned at any time using the assignment operator
"=".
 Datatypes: JavaScript has six primitive datatypes: string, number, boolean, null, undefined, and symbol. It also has a
non-primitive datatype, object, which includes arrays and functions.
 Operators: Operators are used to perform operations on variables and values. JavaScript has several types of
operators including arithmetic, assignment, comparison, logical, and bitwise operators. For example, the arithmetic
operators are used to perform basic arithmetic operations like addition (+), subtraction (-), multiplication (*), and
division (/).

7. What do you mean by event in JavaScript? Give at least two examples of events with their handling.
 In JavaScript, an event refers to any action or occurrence that happens in the browser, such as a user clicking a
button, a page finishing loading, or an element being hovered over. These events can be detected by JavaScript code
and used to trigger certain actions or functions.
 Here are two examples of events with their handling:
 Click event: This event is triggered when a user clicks on a particular HTML element, such as a button or a link. To
handle this event, we can use the "onclick" attribute in HTML or the "addEventListener()" method in JavaScript.
 Example:
<button onclick="myFunction()">Click me</button>
<script>
function myFunction()
{
alert("Button clicked!");
}
</script>

 Mouseover event: This event is triggered when a user moves their mouse over a particular HTML element, such as
an image or a link. To handle this event, we can use the "onmouseover" attribute in HTML or the
"addEventListener()" method in JavaScript.
Example:
<img src="image.jpg" onmouseover="myFunction()">
<script>
function myFunction()
{
alert("Mouse over image!");
}
</script>

8. Write Javascript code to validate a 10 digit mobile number.


 function validateMobileNumber(mobileNumber) {
// Remove any whitespace and dashes from the mobile number
mobileNumber = mobileNumber.replace(/\s/g,'').replace(/-/g,'');

// Check if the mobile number is exactly 10 digits


if (mobileNumber.length !== 10) {
return false;
}

// Check if the mobile number contains only digits


if (!/^\d+$/.test(mobileNumber)) {
return false;
}

// If all checks pass, the mobile number is valid


return true;
}

9. Write Javascript code to print Fibonacci series.


 function fibonacci(numTerms)
{
let fibSeries = [0, 1];

for (let i = 2; i < numTerms; i++) {


fibSeries[i] = fibSeries[i - 1] + fibSeries[i - 2];
}

return fibSeries;
}

console.log(fibonacci(10)); // prints [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


10. Write Javascript code to validate password with following requirements:
(a) Password must have one small letter, one capital letter, one digit and one special character
(b) Password must not have white space
(c) Length must be greater than 8 characters.
 function validatePassword(password) {
// Regex to check for at least one lowercase letter, one uppercase letter, one digit, and one special character
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\=[\]{};':"\\|,.<>/?]).{8,}$/;

// Check for whitespace


if (/\s/.test(password)) {
return false;
}

// Check length
if (password.length < 8) {
return false;
}

// Check for password requirements using regex


if (!regex.test(password)) {
return false;
}

return true;
}

console.log(validatePassword("Pass123@")); // returns true


console.log(validatePassword("pass123@")); // returns false (missing uppercase letter)
console.log(validatePassword("Pass123")); // returns false (missing special character)
console.log(validatePassword("Password 123@")); // returns false (contains whitespace)

11. Write a JavaScript that uses a loop which searches a word in a sentence held in an array, returning the index
of the word.
 function findWordIndex(sentence, word) {
// Split the sentence into an array of words
const words = sentence.split(" ");

// Loop through the words array and look for the target word
for (let i = 0; i < words.length; i++) {
if (words[i] === word) {
// Return the index if the word is found
return i;
}
}

// If the word isn't found, return -1


return -1;
}

const sentence = "This is a sample sentence.";


const word = "sample";
console.log(findWordIndex(sentence, word)); // prints 3
12. Explain pop-up boxes in JavaScript with examples.
 Pop-up boxes in JavaScript are used to display messages or prompts to the user. They are a useful way to provide
feedback or obtain input from the user. There are three types of pop-up boxes in JavaScript:

 Alert Box: An alert box is used to display a message to the user. It has only one button, which dismisses the message
when clicked. Here's an example of how to use an alert box:
alert("Hello, world!");

 Confirm Box: A confirm box is used to ask the user to confirm or cancel an action. It has two buttons, one for
confirming the action and one for canceling it.
Here's an example of how to use a confirm box:
const result = confirm("Are you sure you want to delete this file?");
if (result)
{
// Code to delete the file
}
else
{
// Code to cancel the delete operation
}

 Prompt Box: A prompt box is used to ask the user for input. It has a text field for the user to enter their input, and
two buttons, one for confirming the input and one for canceling it.
Here's an example of how to use a prompt box:
const name = prompt("Please enter your name:");
if (name)
{
alert("Hello, " + name + "!");
}
else
{
alert("You didn't enter a name.");
}

13. Explain HTML forms and form validation in JavaScript.


 HTML forms allow website visitors to input and submit data to a web server. Form elements are used to create form
fields, such as text input boxes, radio buttons, checkboxes, and more.

 To submit a form, the user typically clicks on a submit button, and the data is sent to the server for processing.
However, before the data is sent, it is often necessary to validate the user's input to ensure that it is correct and
complete.

 Form validation can be performed using JavaScript. JavaScript can be used to validate form data both on the client-
side (before the data is sent to the server) and on the server-side (after the data is received by the server).

 Client-side form validation is useful for providing immediate feedback to the user, such as highlighting fields that
are missing or containing incorrect data. JavaScript can be used to validate form data by accessing the form fields
and checking their values against predefined rules. For example, JavaScript can check that an email address is in the
correct format, that a phone number contains only digits, or that a password is strong enough.

 Server-side form validation is essential for security and data integrity. Even if client-side validation is used, it is still
possible for malicious users to bypass it by sending invalid data directly to the server. Server-side validation can
check the submitted data against business rules, database constraints, or other criteria to ensure that it is valid and
safe to use.

 In summary, HTML forms are a crucial part of web development that allow users to input and submit data. Form
validation is necessary to ensure that the submitted data is correct and complete. JavaScript can be used to validate
form data both on the client-side and server-side.

14. Write any FIVE JavaScript Codes of your choice.


 here are five JavaScript code examples:

I. Printing "Hello, World!" to the console:


 console.log("Hello, World!");

II. Getting the current date and time:


 let now = new Date();
console.log(now);

III. Displaying an alert message:


 alert("This is an alert message!");

IV. Creating a function to calculate the area of a rectangle:


 function calculateArea(width, height)
{
let area = width * height; return area;
}
let rectangleArea = calculateArea(5, 10);
console.log(rectangleArea);

V. Changing the color of an HTML element using JavaScript:


 let element = document.getElementById("myElement");
element.style.backgroundColor = "blue";

You might also like