0% found this document useful (0 votes)
1 views17 pages

Javascript Part 1

The document provides a series of JavaScript tasks focusing on variables, data types, operators, conditional statements, and loops. Each task includes code snippets demonstrating how to declare variables, perform operations, check conditions, and iterate through loops. Additionally, it lists tasks for further practice, such as displaying even numbers and creating a multiplication table.

Uploaded by

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

Javascript Part 1

The document provides a series of JavaScript tasks focusing on variables, data types, operators, conditional statements, and loops. Each task includes code snippets demonstrating how to declare variables, perform operations, check conditions, and iterate through loops. Additionally, it lists tasks for further practice, such as displaying even numbers and creating a multiplication table.

Uploaded by

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

JavaScript

Components
INSTRUCTOR: SAMRA SHAHZADI
VARIABLES AND DATA TYPES
Task 1: Declare a variable to
store name and display it

<script>
let name = "Ali";
document.write("Hello, " + name);
</script>
Task 2: Store age in a variable
and check if it's above 18
<script>
let age = 20;
if (age > 18) {
document.write("You are eligible.");
} else {
document.write("You are not eligible.");
}
</script>
Task 3: Concatenate two strings
(first name + last name)
<script>
let firstName = "Sara";
let lastName = "Khan";
let fullName = firstName + " " + lastName;

document.write("Full Name: " + fullName);


</script>
OPERATORS
Task 1:Add Two Numbers
Entered by the User
<script>
let num1 = parseFloat(prompt("Enter first number:"));
let num2 = parseFloat(prompt("Enter second number:"));
let sum = num1 + num2;

document.write("Sum = " + sum);


</script>
Task 2: Use Modulus Operator to
Find Remainder
<script>
let number = parseInt(prompt("Enter number:"));
let divisor = parseInt(prompt("Enter divisor:"));
let remainder = number % divisor;

document.write("Remainder = " + remainder);


</script>
CONDITIONAL STATEMENTS
Task 1: Check if a Number is
Even or Odd
<h3>Enter a number:</h3>
<input type="number" id="numInput">
<button onclick="checkEvenOdd()">Check</button>

<p id="result"></p>
<script>
function checkEvenOdd() {
let num = parseInt(document.getElementById("numInput").value);
let result = document.getElementById("result");

if (isNaN(num)) {
result.textContent = "Please enter a valid number.";
} else if (num % 2 === 0) {
result.textContent = num + " is Even";
} else {
result.textContent = num + " is Odd";
}
}
</script>
Task 2: Create a Simple Login
Check (Username/Password)
<script>
let username = prompt("Enter username:");
let password = prompt("Enter password:");

if (username === "admin" && password === "1234") {


document.write("Login successful!");
} else {
document.write("Invalid credentials.");
}
</script>
Loops
Display "Welcome" 5 Times
<!DOCTYPE html>
<html>
<body>
<div id="output"></div>

<script>
let text = "";
for (let i = 1; i <= 5; i++) {
text += "Welcome<br>";
}
document.getElementById("output").innerHTML = text;
</script>
</body>
</html>
Table of 10
<!DOCTYPE html>
<html>
<body>
<div id="tableOutput"></div>
<script>
let output = "";
for (let i = 1; i <= 10; i++) {
output += `10 x ${i} = ${10 * i}<br>`;
}
document.getElementById("tableOutput").innerHTML = output;
</script>
</body>
</html>
Display Numbers 1 to 10
for (let i = 1; i <= 10; i++) {
document.write(i + "<br>");
}
Tasks List
1. Even Numbers from 1 to 20
2. Reverse Counting (10 to 1)
3. Display "Hello [Name]" 5 Times
4. Sum of First 10 Numbers
5. Table of Any Number (User Input)

You might also like