wt unit2
wt unit2
Logical Operators: Used to perform logical operations like AND (&&), OR (||),
and NOT (!).
Example:
let a = true;
let b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false
3)Arrays in JavaScript:
Arrays in JavaScript are used to store multiple values in a single variable. They
can hold values of
different data types.An array is like a box that holds multiple values in one
variable.
Ex:
let fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // apple
console.log(fruits.length); // 3
// Adding a new item
fruits.push("mango");
console.log(fruits); // ["apple", "banana", "orange", "mango"]
// Removing the last item
fruits.pop();
console.log(fruits); // ["apple", "banana", "orange"]
Q2) Write HTML+ JavaScript program code which makes use of Document
Object Model (DOM).
here's an example HTML and JavaScript program that demonstrates the use of
the Document Object Model (DOM). In this example, we'll create a simple web
page with a button that changes the text of a paragraph when clicked.
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<h2 id="myHeading">Welcome to my webpage!</h2>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
// Access the element using DOM
document.getElementById("myHeading").innerHTML = "You clicked the
button!";
}
</script>
</body>
</html>
Explanation:
getElementById("myHeading") → Finds the <h2> element.
.innerHTML → Changes the content inside the element.
onclick="changeText()" → Runs the function when the button is clicked.
<!DOCTYPE html>
<html>
<head>
<title>Factorial Finder</title>
</head>
<body>
<script>
function findFactorial() {
let num = document.getElementById("numberInput").value;
num = parseInt(num);
let factorial = 1;
if (num < 0) {
alert("Please enter a non-negative number.");
return;
}
</body>
</html>
Q 4) Write html +javascript code to dynamically change the content of
paragraph by using getElementById() method and innerHTML property.
<!DOCTYPE html>
<html>
<head>
<title>Change Paragraph Content</title>
</head>
<body>
<script>
function changeContent() {
// Use DOM to change paragraph content
document.getElementById("myPara").innerHTML = "The paragraph text has
been changed!";
}
</script>
</body>
</html>
Explanation:
getElementById("myPara") finds the paragraph element.
.innerHTML = "..." replaces the paragraph text.
The function runs when the button is clicked.
function generateFibonacci(n) {
let fib = [0, 1]; // Starting two numbers
for (let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib.slice(0, n); // Return first n terms
}
// Example usage:
let series = generateFibonacci(10);
console.log(series); // Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Explanation:
The first two Fibonacci numbers are 0 and 1.
Each next number is the sum of the previous two.
The function returns an array of the first n terms.
Q 7) What is Angular JS? Explain Angular JS directives for data binding with
example.
What is AngularJS?
AngularJS is a front-end JavaScript framework created by Google to make
dynamic web applications easier to build.
It extends HTML with additional features and functionalities, making it more
interactive, especially for data-driven apps.
// Name validation
if (name === "") {
alert("Please enter your name.");
return false;
}
<h2>Registration Form</h2>
1. Hide an element
$("#demo").hide();
o Hides the element with ID demo.
2. Show an element
$(".box").show();
o Shows all elements with class box.
3. Click event
$("button").click(function() {
alert("Button clicked!");
});
o Displays an alert when any <button> is clicked.
4. Change text content
$("#title").text("Hello jQuery!");
o Changes the text of the element with ID title.
5. Change CSS style
$("p").css("color", "green");
o Sets the text color of all <p> elements to green.