0% found this document useful (0 votes)
150 views

Assignment 2

The document contains 10 questions and answers related to JavaScript arrays and strings. Some key points covered are: 1. Initializing an array with flower names and looping through it. 2. Adding and sorting elements in an array. 3. Differences between join() and concat() array methods.

Uploaded by

Asmita Kadam AK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Assignment 2

The document contains 10 questions and answers related to JavaScript arrays and strings. Some key points covered are: 1. Initializing an array with flower names and looping through it. 2. Adding and sorting elements in an array. 3. Differences between join() and concat() array methods.

Uploaded by

Asmita Kadam AK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Q1> Write the javascript that initializes an array called flowers with the names of flowers.

Answer :--

<!DOCTYPE html>

<html>

<head>

<title>Array example </title>

</head>

<body>

<h1>Flower's Name ?</h1>

<p><b> Flower's is given below <br></p>

<script language=Javascript type=text/javascript >

var flower=new Array()

flower[0]="Rose";

flower[1]="Lily";

flower[2]="Lotus";

flower[3]="Sunflower";

flower[4]="MarieGold";

for(i=0;i<flower.length;i++)

document.writeln("<i>"+flower[i]+"<br>")

document.fgColor="Gold";

</script>

</body>

</html>
2> How to add and sort element in array ? Explain with example

Answer :---

<!DOCTYPE html>

<html>

<head>

<title>Array updation</title>

</head>

<body>

<h1>Flower's Name ?</h1>

<p><b> Flower's is given below <br></p>

<script language=Javascript type=text/javascript >

var flower=new Array() flower[0]="Rose";

flower[1]="Lily"; flower[2]="Lotus";

flower[3]="Sunflower"; flower[4]="MarieGold";

flower.push("Turnip");

for(i=0;i<flower.length;i++)

document.write(flower[i]+"<br>"); }

flower.sort();

for(i=0;i<flower.length;i++)

document.writeln("<i>"+flower[i]+"<br>");

} document.fgColor="Gold";

</script>

</body>

</html>

3> Differentiate between join() and concat() methods of array object Answer
:---
Sr no. Join() method() Concat() method

1 Using join() method we can separate array Using concat() method we can’t separate array
element by giving any symbol,number and element by using any type of symbol,number and
character. character.
2 Syntax :-- join(“?”) or join() Syntax :-- conact()

3 Here default separator is (,) Here default separator is also (,)

4 Ex :-- fruit.join(“:”) Ex fruit.concat();

4> Write a javascript that will replace following specified value with another value in string.

String =”I will fail”

Replace=”I will pass”

Answer :--

<!DOCTYPE html>

<html>

<head>

<title>String Manipulation</title>

</head>

<body>

<h1><center><b> <marquee>String Manipulation </marquee></b></center></h1>

<script language="Javascript" type="text/javascript"> var myFirstName="I will fail";

document.write("String before replacing word :"+myFirstName+"<br>");

document.write("String after replacing some words :"+myFirstName.replace("fail","pass"));

</script>

</body>

</html>

5> write a javascript program to call function from html


Answer :--- <!DOCTYPE

html>

<html>

<head>

<title>Age vali</title>

<script language="Javascript" type="text/javascript">

function salary_emp()

var salary=4000*1.5 alert("YOUR NEW

SALARY IS "+salary); document.write("YOUR

NEW SALARY IS "+salary)

</script>

</head>

<body>

<h1> FUNCTION WITHOUT ARGUMENT</h1>

<script language="Javascript " type="text/javascript">

salary_emp() </script>

</body>

</html>

6> write a javascript program to change the case of string

Answer :-
<!DOCTYPE html>

<html>

<head>

<title>String Manipulation</title>

</head>

<body>

<h1><center><b> <marquee>String Manipulation </marquee></b></center></h1>

<script language="Javascript" type="text/javascript">

var myFirstName="I will fail";

// change the case of string

document.writeln("NAME IN UPPERCASE :"+myFirstName.toUpperCase()+"<br>");

document.writeln("NAME IN LOWERCASE :"+myFirstName.toLowerCase()+"<br>");

</script>

</body>

</html>

7> explain javascript method to find Unicode of a character of and find character from Unicode.

Answer :--

To find Unicode of particular character we have charCodeAt(ascii code )method and to find character of
from ascii code we have fromCharCode(char) method.
Example :--

<!DOCTYPE html>

<html>

<head>

<title>String Manipulation</title>

</head>

<body>

<h1><center><b> <marquee>String Manipulation </marquee></b></center></h1>

<script language="Javascript" type="text/javascript">

var c="w";

// change the case of string

document.write("Returned ascii/unicode code :"+c.charCodeAt()+"<br>");

document.write("Returned character is :"+String.fromCharCode(119));

</script>

</body>

</html>

8> Write a javascript code to display 5 elements of array in sorted array

<!DOCTYPE html>

<html>

<head>
<title>ARRAY</title>

</head>

<body>

<h1> <center> ARRAY SORTING </center></h1>

<script language="Javascript " type="text/javascript">

var fruit=new Array(); fruit[0]="PINEAPPLE"

fruit[1]="APPLE"

fruit[2]="WATERMELON"

fruit[3]="ORANGE" fruit[4]="GRAPES"

fruit[5]="COCONUT"

fruit.sort();

for(i=0;i<fruit.length;i++)

document.write(fruit[i]+"<br>");

</script>

</body>

</html>

9> write a javascript function to insert a string at within a string at particular position.

Answer :-- <!DOCTYPE html>

<html>

<head>

<title>String Manipulation</title>

<script language="Javascript" type="text/javascript">

function string1()
{

var email="[email protected]";

document.write("My email is :"+email.substring(6,20)+"<br>");

</script>

</head>

<body>

<h1>String Manipulation</h1>

<script type="text/javascript">

string1()

</script>

</body>

</html>

10> Write the use of charAt() and indexOf() with syntax and example

Answer :--

*charAt(index):-- this method is responsible for return a character from particular index.

*indexOf(char) :-- This method is responsible for return a index from particular character.

<!DOCTYPE html>

<html>

<head>

<title>String Manipulation</title>

</head>

<body>

<h1><center><b> <marquee>String Manipulation </marquee></b></center></h1>


<script language="Javascript" type="text/javascript"> var

myLastName="Arya"; document.write("Index of r character

:"+myLastName.indexOf('r')+'<br>'); document.write("character of index

3 :"+myLastName.charAt(3));

</script>

</body>

</html>

10> write a javascript function that concatenate the two strings.

Answer :---

<!DOCTYPE html>

<html>

<head>

<title>String Manipulation</title>

</head>

<body>

<h1><center><b> <marquee>String Manipulation </marquee></b></center></h1>

<script language="Javascript" type="text/javascript">

var myFirstName="Dinkar"; var myLastName="

Arya";

document.write("My name is : "+myFirstName+myLastName+"<br>");

</script>

</body>
</html>

You might also like