CSS Chapter 2Notes
CSS Chapter 2Notes
Unit Outcomes(UOs)
2a. Create array to solve the given problem.
2b. Perform the specified string manipulation operation on the given String.
2c. Develop JavaScript to implement the given function.
2d. Develop JavaScript to convert the given function.
2e. Develop JavaScript to convert the given character to Unicode and Vice-Versa.
Contents
2.1 Array-declaring an Array, Initializing an Array, defining an Array elements, Looping an array, Adding an
Array element, sorting an Array element, combining n Array element into a String, changing elements of an
Array, object as associative Array.
2.2 Function – Defining a function, writing a function, adding as arguments, scope of variable and
arguments.
2.3 Calling a function – calling a function with or without an argument, calling function from HTML,
function calling another function, returning a value from a function.
2.4 String – manipulate a string, joining a string, retrieving a character from a given position of character in
a string, dividing text, coping a substring, converting, string to number and numbers to string, changing the
case of string, finding a Unicode of a character – charCodeAt(), fromCharCode()
2 Marks Questions
2. State the meaning of "Defining a function". Explain with the help of an example.
• A function is a block of code that takes some input to perform some certain computation.
• The main purpose of the function is to put commonly used or repeatedly used task in a function, so
instead of writing the code again and again we can call it instead.
• The function can be defined as followed:
Syntax:
function func_name(parameter1 ,parameter2,…,parametern)
{
//code
}
Example:
<script>
function add(num1,num2)
{
return num1 + num2;
}
add(1,2);
</script>
OR
3. Give syntax of and explain function in JavaScript with suitable example
Function is a collection of one or more statements written to execute a specific task.
Syntax to define a function:
function function_name([Arguments])
{
Statement block;
[return statement;]
}
Example:
function display ( )
{
alert (―WELCOME TO JAVASCRIPT‖);
}
4. Write a JavaScript that initialize an array called flowers with the names of 3 flowers. The script
then display array elements.
<html>
<head>
<title>Display Array Elements</title>
</head>
<body>
<script>
var flowers = new Array();
flowers[0] = 'Rose ';
flowers[1] = 'Mogra';
flowers[2] = 'Hibiscus';
for (var i = 0; i < flowers.length; i++)
{
document.write(flowers[i] + '<br>');
}
</script>
</body>
</html>
7. Write a program to Accept the marks of 10 subjects from the user and store it in array. Sort them
and display
<html>
<body>
<h2>Accept and Display the marks</h2>
<script>
a = new Array();
length=prompt("For how many subjects do you want to enter a
marks?");
alert("Enter Marks of Subjects")
for(i=0;i<length;i++)
{
a[i]=prompt("Enter marks of subject"+i);
}
document.write("<br/><br/>The entered subjects marks are<br\>");
for(i=0;i<length;i++)
{
document.write("Marks of subject"+i+"is :"+a[i]);
document.write("</br>");
}
document.write("<br/><br/>The array sorted subjects mareks
are<br\>");
a.sort();
document.write("<br/>The element in the array are<br\>");
for(i=0;i<length;i++)
{
document.write("Marks of subject"+i+"is :"+a[i]);
document.write("</br>"); }
</script>
</body>
11. Write the use of charAt() method and indexOf() method with syntax and example.
charAt()
• The charAt() method requires one argument i.e is the index of the character thatyou want
to copy.
Syntax:
var SingleCharacter = NameOfStringObject.charAt(index);
Example:
var FirstName = 'Bob';
var Character = FirstName.charAt(0); //o/p B
indexOf()
• The indexOf() method returns the index of the character passed to it as an
argument. If the character is not in the string, this method returns –1.
Syntax:
var indexValue = string.indexOf('character');
Example:
var FirstName = 'Bob';
var IndexValue = FirstName.indexOf('o'); //o/p index as 1
12. Difference between concat() and join() method of array with example.
concat() join()
Array elements can be combined byusing Array elements can be combined byusing join()
concat() method of Array object. method of Array object.
The concat() method separates eachvalue The join() method also uses a commato
with a comma. separate values, but you can specify a character
other than a comma to separate values.
Eg: Eg:
var str = cars.concat() var str = cars.join(' ')
The value of str is The value of str in this case is
'BMW, Audi, Maruti' 'BMW Audi Maruti'
14. Write a JavaScript that will replace following specified value with another value in the string
String=”I will fail” replace “fail” by “pass”.
<html>
<head>
<body>
<script>
var myStr = ‘I will fail’;
var newStr = myStr.replace(fail, "pass");
document.write(newStr);
</script>
</body>
</head>
</html>
4 Marks Questions
15. Differentiate between substring() and substr() method of a string class. Give Suitable example of
each.
Example:
<script>
var a="Javascript";
document.write("Using substring()="+a.substring(2,6));
document.write("<br>Using substr()="+a.substr(2,6));
</script>
Output:
Using substring()=vasc
Using substr()=vascri
18. Write a JavaScript function that checks whether a passed string is palindrome or not.
function isPalindrome(str) {
str = str.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
return str === str.split('').reverse().join('');
}
console.log(isPalindrome("A man, a plan, a canal, Panama")); //
Output: true
console.log(isPalindrome("racecar")); // Output: true
console.log(isPalindrome("hello")); // Output: false
17. Explain how to add and sort elements in array with suitable example.
Adding Elements to an Array:
In JavaScript, you can add elements to an array using various methods, such as push(), unshift(), or
direct assignment to a specific index.
Using push():
The push() method adds one or more elements to the end of an array and returns the new length of
the array.
Using unshift():
The unshift() method adds one or more elements to the beginning of an array and returns the new
length of the array.
Using splice():
This 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).
18. Develop JavaScript to convert the given character to Unicode and vice-versa
• JavaScript String charAt() Method
• The JavaScript string charAt() method is used to find out a char value present at the specified index
in a string.
• The index number starts from 0 and goes to n-1, where n is the length of the string. The index value
can't be a negative, greater than or equal to the length of the string.
• Syntax
• The charAt() method is represented by the following syntax:
String.charAt(index)
Example1
<html>
<body>
<title>JavaScript String charAt() Method</title>
<script>
var str="Sandip Polytechnic";
document.writeln(str.charAt(4));
</script>
</body>
</html>
19. write a javascript function to generate fibonacci series till user defined limit
<html>
<head>
<title> Fibonacci Series in JavaScript </title>
</head>
<body>
<script>
// declaration of the variables
var n1 = 0, n2 = 1, next_num, i;
var num = parseInt (prompt (" Enter the limit for Fibonacci Series "));
document.write( "Fibonacci Series: ");
for ( i = 1; i <= num; i++)
{ document.write (" <br> " + n1); // print the n1
next_num = n1 + n2; // sum of n1 and n2 into the next_num
</script>
</body>
</html>
20. Write a JavaScript program that will remove the duplicate element from an array.
<html>
<head>
<script>
function getUnique(arr){
var uniqueArr = [];
// loop through array
for(let i of arr) {
if(uniqueArr.indexOf(i) === -1) {
uniqueArr.push(i);
}
}
document.write(uniqueArr);
}
var array = [1, 2, 3, 2, 3];
// calling the function
// passing array argument
getUnique(array);
</script>
</head>
<body></body>
</html>
21. Write and explain a string functions for converting string to number and number to string
Using the String() function
• This method accepts an integer or floating-point number as a parameter and converts it into
string type.
Syntax:
String(object);
• Using the parseInt() method: JavaScript parseInt() Method is used to accept the string and
radix parameter and convert it into an integer.
Syntax:
parseInt(Value, radix)
23. Write HTML script that displays textboxes for accepting username and password. Write proper
JavaScript such that when the user clicks on submit button
i) All textboxes must get disabled and change the color to ‘RED; and with respective labels
ii) Prompt the error message if the password is less than six characters
<html>
<head>
<script>
function disableTxt()
{
document.getElementById("un").disabled = true;
document.getElementById('un').style.color = "red";
document.getElementById('aaa').style.color = "red";
document.getElementById("pass").disabled = true;
document.getElementById('pass').style.color = "red";
document.getElementById('bbb').style.color = "red";
}
function validateform()
{
var username=document.myform.username.value;
var password=document.myform.password.value;
if (username==null || username==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
</head>
<body>
<form name="myform" method="post" action="" onsubmit="return
validateform()" >
<label id = "aaa">Username:</label>
<input type="text" id="un" name="username"/>
<label id = "bbb">
Password:
</label>
<input type="password" id="pass" name="password"/>
<br>
<br>
<button onclick="disableTxt()">Disable Text field</button>
</form>
</body>
</html>