1. Javascript program to get the first and last item in an array.
Program:
var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];
function firstAndLast(array) {
var firstItem = myArray[0];
var lastItem = myArray[[Link]-1];
var objOutput = {
first : firstItem,
last : lastItem
};
return objOutput;
}
var display = firstAndLast(myArray);
[Link](display);
2. Javascript program to add new elements at the beginning of an
array.
Program:
let Arr = [1, 2, 3, 4, 5];
[Link](`Array Elements Before
adding the element at
start: ${Arr}`);
function myGFG() {
[Link]("6");
[Link](`Array Elements After
adding the element at
start: ${Arr}`);
}
myGFG();
3. Javascript program to remove elements from an array.
Program:
const array = [2, 5, 9];
[Link](array);
const index = [Link](5);
if (index > -1) { // only splice array when item is found
[Link](index, 1); // 2nd parameter means remove one item only
}
[Link](array);
4. Javascript program to replace all occurrences of a string.
Program:
const myString = "Apples pears apples strawberries";
const pattern = /apples/gi;
const replacement = "grapes";
const result = [Link](pattern, replacement);
[Link](result); // grapes pears grapes strawberries
5. R javascript program to check if the given date is weekend.
Program:
<!DOCTYPE HTML>
<html>
<head>
<title>
How to check if the given
date is weekend ?
</title>
</head>
<body>
<h1 style="color:green;">
Scripting Language
</h1>
<p id="GFG_UP">
</p>
<button onclick="gfg_Run()">
Click Here
</button>
<p id="GFG_DOWN">
</p>
<script>
var el_up = [Link]("GFG_UP");
var el_down =
[Link]("GFG_DOWN");
var date = new Date();
el_up.innerHTML = "Click on the button to "
+ "check if the given date is Weekend"
+ " or not.<br>Date = " + date;
function gfg_Run() {
var day = [Link]();
var ans = (day === 6 || day === 0);
if (ans) {
ans = "Today is Weekend.";
} else {
ans = "Today is not Weekend.";
}
el_down.innerHTML = ans;
}
</script>
</body>
</html>
6. Write a javascript codes to handle the following events:
Mouse down On mouse down
Mouse move On mouse move
Mouse out On mouse out
Mouse up On mouse up
Reset On reset
Select On select
Submit On submit
Unload On unload
Mouse over On mouse over
Program:
//Mousedown,Mouseup
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmousedown Event</h2>
<p>Clock the text below!</p>
<p id="myP" onmousedown="mouseDown()"
onmouseup="mouseUp()">
The mouseDown() function sets the color of this text to red.
The mouseUp() function sets the color of this text to blue.
</p>
<script>
function mouseDown()
{ [Link]("myP").[Link] =
"red";
}
function mouseUp()
{ [Link]("myP").[Link] =
"blue";
}
</script>
</body>
</html>
//Mouse move
<!DOCTYPE html>
<html>
<style>
div {
width: 200px;
height: 100px;
border: 1px solid black;
}
</style>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmousemove Event</h2>
<div onmousemove="myFunction(event)"
onmouseout="clearCoor()"></div>
<p>Mouse over the rectangle above, and get the coordinates of your
mouse pointer.</p>
<p>When the mouse is moved over the div, the p element will display
the horizontal and vertical coordinates of your mouse pointer, whose
values are returned from the clientX and clientY properties on the
MouseEvent object.</p>
<p id="demo"></p>
<script>
function myFunction(e) {
let x = [Link];
let y = [Link];
let coor = "Coordinates: (" + x + "," + y + ")";
[Link]("demo").innerHTML = coor;
}
function clearCoor()
{ [Link]("demo").innerHTML =
"";
}
</script>
</body>
</html>
//On mouse Over
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmouseover Event</h2>
<img onmouseover="bigImg(this)" onmouseout="normalImg(this)"
border="0" src="[Link]" alt="Smiley" width="32" height="32">
<p>The function bigImg() is triggered when the user moves the
mouse pointer over the image.</p>
<p>The function normalImg() is triggered when the mouse pointer is
moved out of the image.</p>
<script>
function bigImg(x)
{ [Link] =
"64px"; [Link] =
"64px";
}
function normalImg(x) {
[Link] = "32px";
[Link] = "32px";
}
</script>
</body>
</html>
//
7. Write a simple PERL script that should print the string
“Hello World”.
Program:
#!/usr/bin/perl
# Modules used
use strict;
use warnings;
# Print function
print("Hello World\n");
8. Write a PERL script that prints your name and your area of
interest in VLSI domain and run the script.
9. Write a script that will calculate the circumference of a circle.
Program:
function circle(radius)
{
[Link] = radius;
// area method
[Link] = function
()
{
return [Link] * [Link] * [Link];
};
// perimeter method
[Link] = function ()
{
return 2*[Link]*[Link];
};
}
var c = new circle(3);
[Link]('Area =', [Link]().toFixed(2));
[Link]('perimeter =', [Link]().toFixed(2));
10. Create a copy of the perl script “circle” under lab2 and rename it
to circle_1.Then modify the new Script “circle_1” such that it
prompts for and accept a radius from the person running the program
and then calculates the circumference.
11. Write a script that prompts for and reads two numbers and
prints out the product of the two numbers multiplied together in
separate lines.
Program:
// Creating first variable
let a = 10;
// Creating second variable
let b = 10;
let result = a * b;
// Printing the result
// in console
[Link]("Result :", result);
12. Write a php program to design personal information.
Program:
<?php if(isset($_GET['formsubmit']))
{
$name = $_GET['Sname'];
$surname = $_GET['surname'];
$dob = $_GET['dateOfBirth'];
$rollNo = $_GET['rollNo'];
$phoneNumber =
$_GET['PhoneNumber']; echo 'FirstName:
',$name;
echo '</br>Surname: ',$surname;
echo '</br>Date of Birth: ',$dob;
echo '</br>RollNo: ',$rollNo;
echo '</br>Phone Number: ',
$phoneNumber; echo'</br><hr/>';
}
?>
<!DOCTYPE html>
<html>
<head>
<title> display student details </title>
</head>
<body>
<form>
FirstName: <input type="text" name="Sname"> </br></br>
Surname: <input type="text" name="surname"> </br></br>
dateOfBirth <input type="date" name="dateOfBirth">
</br></br>
RollNumber <input type="text" name="rollNo"> </br></br>
PhoneNumber <input type="number" name="PhoneNumber">
</br></br>
<input type="submit" value="submit" name="formsubmit">
</br></br>
</form>
</body>
</html>
13. Create a php page for login page with sql connection.
Program:
<html>
<head>
<title>PHP login system</title>
// insert [Link] file inside [Link]
<link rel = "stylesheet" type = "text/css" href = "[Link]">
</head>
<body>
<div id = "frm">
<h1>Login</h1>
<form name="f1" action = "[Link]" onsubmit =
"return validation()" method = "POST">
<p>
<label> UserName: </label>
<input type = "text" id ="user" name = "user" />
</p>
<p>
<label> Password: </label>
<input type = "password" id ="pass" name = "pass" />
</p>
<p>
<input type = "submit" id = "btn" value = "Login" />
</p>
</form>
</div>
// validation for empty field
<script>
function validation()
{
var id=[Link];
var ps=[Link];
if([Link]=="" && [Link]=="") {
alert("User Name and Password fields are empty");
return false;
}
else
{
if([Link]=="") {
alert("User Name is empty");
return false;
}
if ([Link]=="")
{ alert("Password field is empty");
return false;
}
}
}
</script>
</body>
</html>
<?php
$host = "localhost";
$user = "root";
$password = '';
$db_name = "javatpoint";
$con = mysqli_connect($host, $user, $password, $db_name);
if(mysqli_connect_errno()) {
die("Failed to connect with MySQL: ". mysqli_connect_error());
}
?>
<?php
include('[Link]');
$username = $_POST['user'];
$password = $_POST['pass'];
//to prevent from mysqli injection
$username = stripcslashes($username);
$password = stripcslashes($password);
$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);
$sql = "select *from login where username = '$username' and
password = '$password'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count == 1){
echo "<h1><center> Login successful </center></h1>";
}
else{
echo "<h1> Login failed. Invalid username or
password.</h1>";
}
?>
14. Write a php program to read from existing file.
Program:
<?php
$filename = "c:\\[Link]";
$fp = fopen($filename, "r");//open file in read mode
$contents = fread($fp, filesize($filename));//read file
echo "<pre>$contents</pre>";//printing data of file
fclose($fp);//close file
?>
15. Write a Ruby program which accepts the user’s first and last
name and print them in reverse order with a space between them.
Program:
puts "Input your first name: "
fname = [Link]
puts "Input your last name: "
lname = [Link]
puts "Hello #{lname} #{fname}"
16. Write a Ruby program to check three numbers and return true if
one or the other is small,but not both. A number is called “small” if
it is in the range 1..0 inclusive.
Program:
def in110(a, b)
return (((a >= 1 && a <= 10) && !(b >= 1 && b <= 10)) || (!
(a >= 1 && a <= 10) && (b >= 1 && b <= 10)));
end
print in110(1, 12),"\n"
print in110(10, 15),"\n"
print in110(11, 25),"\n"
17. Write a Ruby program to check whether a string ‘Java’ appears
at index 1 in a given string.
Program:
def text_test(str)
if(str[1,4] == "Java")
return (str[5, [Link]()]);
else
return str;
end
end
print text_test("ZJavaScript"),"\n"
print text_test("Oldjava")
18. Write a Ruby program to check two given integers and return the
larger value. However if the two values have the same remainder
when divided by 5 then return the smaller value and if the two values
are the same.
Program:
def check_num(a, b)
if(a == b)
return 0
end
if(a % 5 == b % 5)
return (a < b) ? a : b
end
return (a > b) ? a : b
end
print check_num(9, 12),"\n"
print check_num(110, 200),"\n"
print check_num(10, 10)