WT Assignment - 1
WT Assignment - 1
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.
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.
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>
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>
return fibSeries;
}
// Check length
if (password.length < 8) {
return false;
}
return true;
}
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;
}
}
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.");
}
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.