0% found this document useful (0 votes)
10 views7 pages

Question-1: Part (A)

The document contains multiple JavaScript code snippets demonstrating various functionalities such as string manipulation, array duplication, binary conversion, and DOM structure of an HTML document. It also includes SQL commands for creating a database and tables for a university management system, along with PHP code for database connection and fetching employee records. Additionally, it showcases associative arrays in both PHP and JavaScript, illustrating how to access and display user information.

Uploaded by

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

Question-1: Part (A)

The document contains multiple JavaScript code snippets demonstrating various functionalities such as string manipulation, array duplication, binary conversion, and DOM structure of an HTML document. It also includes SQL commands for creating a database and tables for a university management system, along with PHP code for database connection and fetching employee records. Additionally, it showcases associative arrays in both PHP and JavaScript, illustrating how to access and display user information.

Uploaded by

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

Question-1:

Part(a):
<script>
var message="Thanks,";
var userName = "Susan";
var banger="!";
var customMess = message+userName +banger+""+String(42);
alert(customMess);
</script>

Part(b):
<script>
var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var now =new Date();
var theDay=now.getDay();
var nameOftoday=dayNames[(theDay+1)%7];
alert(nameOftoday);
</script>

Part(c):
<script>
function duplicate(arr){
return arr.concat(arr);
}
console.log("Duplicate Array:",duplicate([1,2,3,4,5]));
</script>

Part(d):

<script>
var string = "Welcome to this Javascript Guide!";
function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator); }
var reverseEntireSentence=reverseBySeparator(string, ""); \\ this line reverse all the character in
the string
var reverseEachWord= reverseBySeparator(reverseEntireSentence, ""); \\ reverse each word
within reversed string, so we need to one statement/line if we want to reverse our string
console.log("Reversed Each Word:", reverseEachWord);
console.log("Reversed Entire Sentence:", reverseEntireSentence);
</script>
Part(e):

<script>
function decimalToBinary(digit){
if(digit<0){
return "Invalid Input";
}
else if(digit===0){
return "0";
}
else if (digit >= 1){
if (digit % 2){
return decimalToBinary((digit-1)/2)+1;
}
else{
// Recursively return proceeding binary digits
return decimalToBinary(digit/2)+"0";
}}
else {
return"";}
}
console.log("Binary of -5", decimalToBinary(-5));
console.log("Binary of 0", decimalToBinary(0));
console.log("Binary of 3", decimalToBinary(3));
console.log("Binary of 8", decimalToBinary(8));
console.log("Binary of 1000", decimalToBinary(100));
</script>

Question-2:
<!DOCTYPE html>
<html>
<head>
<title>Modified Document</title>
</head>
<body>
<header>
<h1>Welcome to My Simple Document</h1>
</header>
<div>
<p>There's not much to this, but now It has a header</p>
<div>
<section>
<h2>Additional Sections/h2>
<p>This is some new content in another section.</p>
</section>
<footer>
<p>Thanks for visiting!</p> </footer>
</body>
</html>

DOM structure:
 Level 0: <!DOCTYPE html> (It is a root of document structure but not part of the DOM
tree)
 Level 1: <html> (root of the DOM tree)
 Level 2: <head> <body> (Children of <html> )
 Level 3: <title> (Children of head), <header> <div> <section><footer> (Children of
Body)
 Level 4: text nodes <h1> <p>

HTML

Head Body

Title Header Div Section Footer

H1 P H2 P P
Question-4:
MYSQL:
Create database UniversityManagementSystem;
CREATE TABLE Employee(
employee_id int AUTO_INCREMENT PRIMARY KEY,
name varchar(30) not null,
department varchar(30),
position varchar(30)
);
CREATE TABLE Student(
student_id int AUTO_INCREMENT PRIMARY KEY,
name varchar(30) not null,
course varchar(30),
employee_id int,
FOREIGN key (employee_id) REFERENCES employee(employee_id)
);
CREATE TABLE Faculty(
faculty_id int AUTO_INCREMENT PRIMARY KEY,
name varchar(30) not null,
subject varchar(30),
employee_id int,
FOREIGN key (employee_id)REFERENCES employee(employee_id)
);
Create Database connection with PHP:
<?php
$host="localhost";
$username="root";
$password="";
$dbname="universitymanagementsystem";
$conn=new mysqli($host,$username,$password,$dbname);
if($conn->connect_error){
die("Connection failed:" .$conn->connect_error);
}
else
{
echo "Connection Successful";
}
?>

Fetch Employee Table data:


<?php
$host="localhost";
$username="root";
$password="";
$dbname="universitymanagementsystem";
$conn=new mysqli($host,$username,$password,$dbname);
if($conn->connect_error){
die("Connection failed:" .$conn->connect_error);
}
else
{
echo "Connection Successful";
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Record</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family:'Arial' sans-serif;
}
body {
background-color:#f9f9f9;
color:#333;
padding:20px;
}
table{
border-collapse:collapse;
width:70%;
margin: 20px auto;
}
th,td{
border:1px solid #ddd;
padding:10px;
text-align:center;
}
</style>
</head>
<body>
<h1>Employee Record</h1>
<table>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Department</th>
<th>Position</th>
</tr>
<?php
$result = $conn->query("SELECT * FROM employee");
if($result-> num_rows > 0){
while ($row = $result->fetch_assoc()){
echo "<tr>
<td>".$row['employee_id']."</td>
<td>".$row['name']."</td>
<td>".$row['department']."</td>
<td>".$row['position']."</td>
</tr>";
}
}
else{
echo "<tr><td>No records found.</td></tr>";
}
?>
</table>
</body>
</html>

Question-5:
Associative array in PHP:
<?php
$users=[
"user1"=>["name"=>"Hamza", "email"=>"[email protected]"],
"user2"=>["name"=>"Ali", "email"=>"[email protected]"],
];
echo "user1's email" .$users["$user1"]["$email"]. "<br>";
echo "User Profile" ;
foreach($users as $username =>$details)
{
echo "Username:$username";
foreach($details as $key =>$value){
echo $key .":" .$value. "<br>";
}
}
?>

Associated Array in JavaScript:

<html>
<head>
</head>
<body>
<script>
const users={
user1:{name:"Hamza", email:"[email protected]"},
user2:{name:"Ali", email:"[email protected]"},
};
console.log("User1 email:" +users.user1.email);
console.log("User Profile");
for (var username in users){
console.log("Username:" +username);
for (var key in users[username]){
console.log(`${key.slice(1)}:${users[username][key]}`);
}
console.log("");
}
</script>
</body>
</html>

You might also like