Sept Reexam CSS 2022 (1)
Sept Reexam CSS 2022 (1)
Property:
A Property is a value or set of values that is the member of an object.
Example:
var person = new Object();
person.firstName = “Hhh";
person.age = 10;
In above example,
Person is an object and firstname and age are properites.
(b) What is operator? Which type of operators are used in Java script.
<html>
<body>
<script>
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()
+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" +
today.getSeconds();
var dateTime = date+' '+time;
document.write(dateTime);
</script>
</body>
</html>
Output:
2023-1-12 14:54:26
Checkbox:
<input> elements of type checkbox are rendered by default as boxes that are
checked (ticked) when activated. A checkbox allows you to select single values
for submission in a form (or not).
Syntax for creating checkbox is:
<input type="checkbox" id="myCheck" onclick="myFunction()">
A checkbox can have only two states:
1. Checked
2. Unchecked
g) State and explain what is a session cookie.
The session cookie is a server-specific cookie that cannot be passed to any
machine other than the one that generated the cookie. The session cookie
allows the browser to re-identify itself to the single, unique server to which the
client had previously authenticated.
The alert() method displays an alert box with a message and an OK button.
The alert() method is used when you want information to come through to
the user.
Syntax:
alert(message)
example:
<html>
<body>
<h1>The Window Object</h1>
<h2>The alert() Method</h2>
<p>Click the button to see line-breaks in an alert box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
alert("Hello\nHow are you?");
}
</script>
</body>
</html>
(b) Explain getter and setter properties in Java script with suitable
example.
Also known as Javascript accessors.
Getters and setters allow you to control how important variables are accessed
and updated in your code.
JavaScript can secure better data quality when using getters and setters.
example: Getters and setters allow you to get and set properties via methods.
<script>
var person = {
firstName: 'Chirag',
lastName: 'Shetty',
get fullName()
{
return this.firstName + ' ' + this.lastName;
},
set fullName (name)
{
var words = name.split(' ');
this.firstName = words[0];
this.lastName = words[1];
}
}
document.write(person.fullName); //Getters and setters allow you to get and
set properties via methods.
document.write("<br>"+"before using set fullname()"+"<br>");
person.fullName = 'Yash Desai'; //Set a property using set
document.writeln(person.firstName); // Yash
document.write(person.lastName); // Desai
</script>
Output:
Chirag Shetty before using set fullname() Yash Desai
Output:
square root of 16 is =4
floor of 16.877 is=16
ceil of 16.877 is=17
ceil of 16.241 is=17
sin of 90 is=0.8939966636005579
Output:
informationtechnology
concat() join()
The concat() method concatenates (joins) The join() method returns an array
two or more arrays. as a string.
The concat() method returns a new array,
containing the joined arrays.
The concat() method separates each value Any separator can be specified. The
with a comma only. default is comma (,).
Syntax: Syntax:
array1.concat(array2, array3, ..., arrayX) array.join(separator)
Example: Example:
<script> <script>
const arr1 = ["CO", "IF"]; var fruits = ["Banana", "Orange",
"Apple", "Mango"];
const arr2 = ["CM", "AI",4];
var text = fruits.join();
const arr = arr1.concat(arr1, arr2);
document.write(text);
document.write(arr);
var text1 = fruits.join("$$");
</script>
document.write("<br>"+text1);
</script>
(c) How to add and sort elements in array? Explain with example.
The array.sort() is an inbuilt method in JavaScript which is used to sort the
array.
Syntax:
array.sort();
Here array is the set of values which is going to be sorted.
Example:
<script>
var arr1 =["Red", "red", 200,"Blue",100];
document.write("Before sorting arra1=" + arr1);
document.write("<br>After sorting arra1="+ arr1.sort());
</script>
Output:
Before sorting arra1=Red,red,200,Blue,100
After sorting arra1=100,200,Blue,Red,red
Adding elements into an array:
Method1:
The easiest way to add a new element to an array is using the push() method.
The push() method adds new items to the end of an array, and returns the new
length.
Syntax:
array.push(item1, item2, ..., itemX);
Example:
var fruits = [ "Banana", "Orange", "Apple", "Mango” ];
fruits.push( "Lemon” ); // adds a new element (Lemon) to fruits
Method 2:
The unshift() method adds one or more elements to the beginning of
an array and returns the new length of the array.
Syntax:
array.unshift(item1, item2, ..., itemX);
Example:
var fruits = [ "Banana", "Orange", "Apple", "Mango” ];
fruits.unshift( "Lemon","Pineapple” );
Method 3:
The length property provides an easy way to append a new element to an array:
Example:
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits+"<br>");
fruits[fruits.length] = "Kiwi";
document.write(fruits+"<br>");
fruits[fruits.length] = "Chikoo";
document.write(fruits);
</script>
Output:
Banana,Orange,Apple,Mango
Banana,Orange,Apple,Mango,Kiwi
Banana,Orange,Apple,Mango,Kiwi,Chikoo
Method 4:
The splice() method can be used to add new items to an array, and
removes elements from an array.
Syntax:
arr.splice(start_index,removed_elements, list_of_elemnts_to_be_added);
Parameter:
The first parameter defines the position where new elements
should be added (spliced in).
The second parameter defines how many elements should be removed.
The list_of_elemnts_to_be_added parameter define the new elements to
be added(optional).
The splice() method can be used to add new items to an array, and
removes elements from an array.
Syntax:
arr.splice(start_index,removed_elements, list_of_elemnts_to_be_added);
Parameter:
The first parameter defines the position where new elements
should be added (spliced in).
The second parameter defines how many elements should be removed.
The list_of_elemnts_to_be_added parameter define the new elements to
be added(optional).
Example:
<script>
var fruits = ["Banana", "Watermelon", "Chikoo", "Mango", "Orange", "Apple"];
document.write(fruits+"<br>");
fruits.splice(2,2, "Lemon", "Kiwi");
document.write(fruits+"<br>");
fruits.splice(0,2); //removes first 2 elements from array
document.write(fruits+"<br>");
</script>
Output:
Banana,Watermelon,Chikoo,Mango,Orange,Apple
Banana,Watermelon,Lemon,Kiwi,Orange,Apple
Lemon,Kiwi,Orange,Apple
(d) How to read and write cookie in javascript? Explain with example.
Read a Cookie with JavaScript
var x = document.cookie;
Write a Cookie with JavaScript
You can access the cookie like this which will return all the cookies saved for the
current domain.
document.cookie=x;
Example:
<html>
<head>
<script>
function writeCookie()
{
with(document.myform)
{
document.cookie="Name=" + person.value + ";"
alert("Cookie written");
}
}
function readCookie()
{
var x;
if(document.cookie=="")
x="";
else
x=document.cookie;
document.write(x);
}
</script>
</head>
<body>
<form name="myform" action="">
Enter your name:
<input type="text" name="person"><br>
<input type="Reset" value="Set Cookie" type="button"
onclick="writeCookie()">
<input type="Reset" value="Get Cookie" type="button"
onclick="readCookie()">
</form>
</body>
</html>
Syntax
new RegExp("[^xyz]")
or simply:
/[^xyz]/
Syntax with modifiers
new RegExp("[^xyz]", "g")
or simply:
/\[^xyz]/g
Example:
<html> <script>
function check()
{
var exp=/[^\*]/;
var res=exp.test(document.getElementById("txt1").value);
document.getElementById("demo1").innerHTML=res;
}
</script>
<body>
Enter text:<textarea id="txt1"></textarea>
<input type="button" onclick="check()" value="Check">
<p id="demo1"></p>
</body>
</html>
(b) What is status bar? How to create status bar in Javascript.
The status property of the Window interface was originally intended to set the
text in the status bar at the bottom of the browser window. However, the HTML
standard now requires setting window.status to have no effect on the text
displayed in the status bar.
Syntax:
window.status = string;
var value = window.status;
<html>
<head>
<title>JavaScript Status Bar</title></head>
<body>
<a href="http://www.vpt.edu.in"
onMouseOver="window.status='Vidyalankar';return true"
onMouseOut="window.status='';return true">
Vidyalankar
</a>
</body>
</html>
Output:
<script>
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n)
{
showDivs(slideIndex += n);
}
function showDivs(n)
{
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length)
{
slideIndex = 1
}
if (n < 1)
{
slideIndex = x.length
}
for (i = 0; i < x.length; i++)
{
x[i].style.display = "none";
}
x[slideIndex-1].style.display = "block";
}
</script>
</body>
</html>
(d) Describe how to replace text using Regular Expression with example.
● you can also use a regular expression to replace portions of the text by
using the replace() method.
● The replace() method requires two parameters: a regular expression and
the
● replacement text.
The replace() method searches a string for a specified value, or a regular exp
Syntax
string.replace(searchvalue, newvalue)
Example:
<html>
<script>
function check()
{
var exp=/:/g;
var str=document.getElementById("txt").value;
var res=str.replace(exp,"-");
document.getElementById("demo1").innerHTML=res;
}
</script>
<body>
Enter text:<textarea id="txt"></textarea>
<input type="button" onclick="check()" value="check">
<p id="demo1"></p>
</body>
</html>
Output:
(e) What is context menu? How to create it? Explain with example.
When we click the right mouse button on our desktop, a menu-like box appears
and this box is called the context menu. In JavaScript, a context menu event runs
when a user tries to open a context menu. This can be done by clicking the right
mouse button.
Example:
<html>
<head>
<style>
div {
background: yellow;
border: 1px solid black;
padding: 10px;
}
</style>
</head>
<body>
<div contextmenu="mymenu">
<p>Right-click inside this box to see the context menu!
</div>
</body>
</html>
Output:
<html>
<body>
<form action=" ">
EmpId: <input type="text" id="eid" onchange="highlightid(this)">
<br><br>
Name: <input type="text" id="name"
onchange="highlightname(this)">
<br><br>
Designation: <input type="text" id="des"
onchange="highlightdes(this)"> <br><br>
</form>
<p>Click the button to get the details:</p>
<button onclick="myFunction()">Details</button>
<BR>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
function myFunction()
{
var y = document.getElementById("eid").value;
document.getElementById("demo").innerHTML = y;
var x = document.getElementById("name").value;
document.getElementById("demo1").innerHTML = x;
var z = document.getElementById("des").value;
document.getElementById("demo2").innerHTML = z;
var g=y.concat(x);
var k=g.concat("@yahoo.com");
document.getElementById("demo3").innerHTML = k;
}
function highlightid(y)
{
y.style.color="blue";
y.style.backgroundColor="pink";
}
function highlightname(x)
{
x.style.color="blue";
x.style.backgroundColor="pink";
}
function highlightdes(z)
{
z.style.color="blue";
z.style.backgroundColor="pink";
}
</script>
</body>
</html>
Output:
function start()
{
obj.style.left=parseInt(obj.style.left)+ 10 + 'px';
animate=setTimeout(start,10);
}
function stop()
{
clearTimeout(animate);
obj.style.left='0 px';
}
window.onload=init;
</script>
</head>
<body>
<img id="car" src="car.jpg">
<br><br>
<input value="Start" type="button" onclick="start()"/>
When user will select the fruits radio button, the option list should present only
the fruits names to user and when user will select the vegetable radio button, the
option list should present only the vegetable names to user so that user can
select an appropriate element of interest.
<html>
<body>
<html>
<script type="text/javascript">
function modifyList(x)
{
with(document.forms.myform)
{
if(x ==1)
{
optionList[0].text="Kiwi";
optionList[0].value=1;
optionList[1].text="Pine-Apple ";
optionList[1].value=2;
optionList[2].text="Apple";
optionList[2].value=3;
}
if(x ==2)
{
optionList[0].text="Tomato";
optionList[0].value=1;
optionList[1].text="Onion ";
optionList[1].value=2;
optionList[2].text="Cabbage ";
optionList[2].value=3;
}
}
}
</script>
</head>
<body>
<form name="myform" action=" " method="post">
<select name="optionList" size="3">
<option value=1>Kiwi
<option value=1>Pine-Apple
<option value=1>Apple
</select>
<br>
<input type="radio" name="grp1" value=1 checked="true"
onclick="modifyList(this.value)"> Fruits
(b) Write a javascript to create rotating banner ads with URL links..
Code:
<html>
<head>
<title>Link Banner Ads</title>
<script language="Javascript" type="text/javascript">
#myUL {
margin: 0;
padding: 0;
}
.caret {
cursor: pointer;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none;
}
.caret::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
.caret-down::before {
-ms-transform: rotate(90deg); /* IE 9 */
-webkit-transform: rotate(90deg); /* Safari */'
transform: rotate(90deg);
}
.nested {
display: none;
}
.active {
display: block;
}
</style>
</head>
<body>
<ul id="myUL">
<li><span class="caret">India</span>
<ul class="nested">
<li>Karnataka</li>
<li>Tamilnaadu</li>
<li><span class="caret">Maharashtra</span>
<ul class="nested">
<li>Mumbai</li>
<li>Pune</li>
<li><span class="caret">Navi Mumbai</span>
<ul class="nested">
<li>Nerul</li>
<li>Vashi</li>
<li>Panvel</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<script>
var toggler = document.getElementsByClassName("caret");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
</script>
</body>
</html>
Output: