wt_unit3
wt_unit3
File
.java .js
Extension
2. What is JavaScript? List its benefits and problems. Are OOP concepts
incorporated in JavaScript? How are objects handled in it?
JavaScript is a lightweight, interpreted scripting language primarily used to create dynamic and
interactive effects within web browsers.
Benefits:
Simple and easy to learn [2]
Fast execution (client-side)
Reduces server load (validates data in browser)
Highly interoperable (can be embedded in HTML and other languages)
Enables dynamic, interactive web pages
Problems:
Security risks (runs on client, can be exploited)
Browser compatibility issues for some features
No support for multithreading or multiprocessing
Source code is visible to users
OOP Concepts in JavaScript:
JavaScript supports object-oriented programming, including encapsulation, inheritance (via
prototypes), and, from ES6, classes [3] .
Objects are created using object literals, constructor functions, or ES6 classes.
Object Handling Example:
// Object literal
let person = {
name: "Alice",
greet: function() { console.log("Hello, " + this.name); }
};
person.greet();
// Constructor function
function Car(model) {
this.model = model;
}
let car1 = new Car("Toyota");
console.log(car1.model);
// ES6 class
class Animal {
constructor(type) { this.type = type; }
}
let dog = new Animal("Dog");
console.log(dog.type);
External:
<script src="script.js"></script>
Constructor function:
function Person(name) { this.name = name; }
let p = new Person("Bob");
ES6 class:
class Car { constructor(model) { this.model = model; } }
let myCar = new Car("Honda");
Modification:
Add or update property:
obj.city = "Bangalore";
obj.age = 26;
Delete property:
delete obj.city;
document.write("Welcome to JavaScript!");
console.log("Debug info");
document.getElementById("output").innerText = "Result";
Keyboard Input:
prompt() for user input:
let name = prompt("Enter your name:");
alert("Hello, " + name);
10. Which are the methods used for accepting inputs from the keyboard and for
displaying outputs on the screen?
Input: prompt()
Output: alert(), document.write(), console.log(), and updating DOM elements
unshift(): Adds one or more elements to the beginning of an array and returns the new
length.
arr.unshift(0); // returns 3, arr is now [0,1,2]
search(): Returns the index of the first match of a regex in a string, or -1 if not found.
"abc123".search(/\d+/); // 3
12. Explain the interactive dialogue boxes used in JavaScript.
alert(): Displays a message with an OK button.
alert("This is an alert!");
prompt(): Displays a message with a text box for user input, returns the input or null.
let name = prompt("Enter your name:");
13. Give examples for the different ways an array object can be created in
JavaScript, and also write HTML document and JavaScript code to sort 'N' given
values using a sorting technique.
Array Creation:
<!DOCTYPE html>
<html>
<body>
<input id="numbers" value="4,2,7,1,5">
<button onclick="sortNumbers()">Sort</button>
<div id="result"></div>
<script>
function sortNumbers() {
let input = document.getElementById('numbers').value;
let arr = input.split(',').map(Number);
arr.sort((a, b) => a - b);
document.getElementById('result').innerText = arr.join(', ');
}
</script>
</body>
</html>
14. Describe functions in JavaScript. Write an HTML document and JavaScript
function to compute and print factorial of a number, reverse a number, and
generate Fibonacci numbers.
Function Examples:
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
function reverseNumber(n) {
return Number(String(n).split('').reverse().join(''));
}
function fibonacci(n) {
let arr = [0, 1];
for (let i = 2; i < n; i++) arr[i] = arr[i - 1] + arr[i - 2];
return arr.slice(0, n);
}
HTML Example:
<!DOCTYPE html>
<html>
<body>
<input id="num" type="number">
<button onclick="showResults()">Compute</button>
<div id="output"></div>
<script>
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
function reverseNumber(n) {
return Number(String(n).split('').reverse().join(''));
}
function fibonacci(n) {
let arr = [0, 1];
for (let i = 2; i < n; i++) arr[i] = arr[i - 1] + arr[i - 2];
return arr.slice(0, n);
}
function showResults() {
let n = parseInt(document.getElementById('num').value);
let fact = factorial(n);
let rev = reverseNumber(n);
let fib = fibonacci(n);
document.getElementById('output').innerHTML =
"Factorial: " + fact + "<br>" +
"Reverse: " + rev + "<br>" +
"Fibonacci: " + fib.join(', ');
}
</script>
</body>
</html>
15. Write a JavaScript code that displays the text "REVA UNIVERSITY" with
increasing font size in the interval of 100ms in blue color when the font size
reaches 50pt, it should stop increasing.
<!DOCTYPE html>
<html>
<body>
<div id="text" style="color:blue;">REVA UNIVERSITY</div>
<script>
let size = 10;
let interval = setInterval(function() {
if (size >= 50) clearInterval(interval);
document.getElementById('text').style.fontSize = size + 'pt';
size++;
}, 100);
</script>
</body>
</html>
16. Write a program in JavaScript to accept three numbers using prompt and
calculate the largest of three numbers.
17. Create a form for collecting student details and create a JavaScript code to
check the Name of the student with less than 50 characters and it should accept
only English alphabetic characters.
<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validate()">
Name: <input type="text" id="name"><br>
<input type="submit" value="Submit">
</form>
<script>
function validate() {
let name = document.getElementById('name').value;
if (name.length > 50) {
alert("Name must be less than 50 characters");
return false;
}
if (!/^[a-zA-Z]+$/.test(name)) {
alert("Name must contain only alphabets");
return false;
}
return true;
}
</script>
</body>
</html>
18. Explain various methods in JavaScript to access DOM Nodes with examples.
getElementById: Selects an element by its ID.
let el = document.getElementById('myId');
19. A client is a web browser through which users make requests... Explain the
difference between client and server.
Client (Web Browser) Server
Handles UI, user input, rendering Handles data processing, storage, business logic
21. Explain the various ways of linking JavaScript code with HTML documents.
Directly in HTML: Using <script> tags in the <head> or <body>.
External File: Using <script src="filename.js"></script> in the HTML.
Modification:
arr[^0].name = "Alicia";
Accessing Properties:
24. Explain Empty Field Validation and Number Validation with suitable example.
Empty Field Validation:
if (input.value === "") {
alert("Field cannot be empty");
}
Number Validation:
let age = input.value;
if (isNaN(age) || age < 1 || age > 100) {
alert("Enter a valid age between 1 and 100");
}
25. Explain the following two classes of Event: (1) Form Events (2) Mouse Events
Form Events: Triggered by user interaction with forms (e.g., onsubmit, onchange, oninput).
Mouse Events: Triggered by mouse actions (e.g., onclick, onmouseover, onmouseout,
onmousedown, onmouseup).
26. Write a program in JavaScript to accept three numbers using prompt and
calculate the largest of three numbers.
If you need even more detail or code for any specific question, let me know the question
number.
⁂
1. WhatsApp-Image-2025-05-11-at-19.15.12-1.jpg
2. WhatsApp-Image-2025-05-11-at-19.15.12.jpg
3. WhatsApp-Image-2025-05-11-at-19.15.13.jpg