QB Answer - Chap2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Unit – II Array, Function and String (Marks :14)

Remember level Understand level Apply level

2 4 8

2marks
1. Write a JavaScript that initializes an array called “Fruits” with names of five fruits. The script
then displays the array in a message box.
<script>
var fruits = ["Mango","Watermelon","Dragonfruit","Guava","Apple"];
alert(fruits);
</script>
2. Write a JavaScript function that checks whether a passed string is palindrome or not
<script type="text/javascript">
let str = prompt("Enter a string")
str = str.toLowerCase()
let rev = ''
for(let i=str.length-1;i>=0;i--){
rev+=str[i]
}
if(rev==str)
document.write("Palindrome!")
else{
document.write("Not Palindrome!"+"<br>")
document.write(rev+"<br>")
document.write(str+"<br>")
}
</script>
3. Write a JavaScript function to count the number of vowels in a given string.
<script>
const string = prompt("enter String")
let vowels = 0;
for (let i = 0; i < string.length; i++) {
switch (string[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++
break;
}
}
document.write(vowels)
</script>
4. Write a function that prompts the user for a color and uses what they select to set the
background color of the new webpage opened.
<html>
<head> </head>
<body></body>
<script>
let color = prompt("Enter any color");
var myNewWindow = window.open();
myNewWindow.document.body.style.background = color;
</script>
</html>
5. Develop JavaScript to convert the given character to Unicode and vice versa.
<script>
var chr = “abc”
var charcode = chr.charCodeAt(2)
document.write("converting char to code " +charcode+"<br>")
const char = String.fromCharCode(97)
document.write("converting code to char "+char)
</script>
6. State and explain any two properties of array object
7. Write a JavaScript function to insert a string within a string at a particular position
<script>
var str = 'GeeksGeeks'
function insert(str)
{
var subStr = 'for'
var pos = 5
document.write([str.slice(0, pos), subStr, str.slice(pos)].join(''))
}
insert(str)
</script>

8. Write a Java script that initializes an array called flowers with the names of 3 flowers. The
script then displays array elements.
<script>
var flowers = ["Rose","Mogra","Tulip"];
document.write(flowers);
</script>

9. Write Javascript to call function from HTML.


<html>
<head>
</head>
<body >
<button onclick="helloWorld()">
Click me
</button>
</body>
<script>
function helloWorld(){
document.write("Printing hello world from function");
}
</script>
</html>
Or
<html>
<head>
<title>Function demo</title>
<body>
<script>
function open()
{
alert(“Welcome”);
}
function close()
{
alert(“Thank You”);
}
</script> </body> </html>

10. Write the use of chatAt() and indexof() with syntax and example.
11. Write a Java Script code to display 5 elements of array in sorted order.
<script>
var arr = ["e","g","a","r","v"];
arr.sort()
for(i in arr)
document.write(arr[i]+"<br>")
</script>
12. State the use of method in JavaScript with the help of suitable example.
13. Write a program using sort method of array object.
<script>
var arr = ["e","g","a","r","v"];
arr.sort()
for(i in arr)
document.write(arr[i]+"<br>")
</script>
14. Write a JavaScript that initializes an array called Colors with the names of Colors and
display the array elements.
<script>
var colors = ["Red","Green","Blue",”Pink”,”Magenta”,”orange”];
document.write(flowers);
</script>

15. Explain calling a function with arguments in JavaScript with example.

Sample paper
16. Describe all the tokens of the following statements :
i. document.bgColor
ii. document.write()
17. State the use of following methods.
i. charCodeAt()
ii. fromCharCode()
18. Differentiate between prompt() and alert() methods.
19. State and explain any two properties of array object.
20. Write a JavaScript that initializes an array called “Fruits” with names of five fruits. The script
then displays the array in a message box.
<script>
var fruits = ["Mango","Watermelon","Dragonfruit","Guava","Apple"];
alert(fruits);
</script>
21. Enlist and explain the use of any two Intrinsic JavaScript functions.
22. Write syntax of and explain prompt method in JavaScript with the help of suitable example
23. Write a JavaScript function to insert a string within a string at a particular position
<script>
var str = 'GeeksGeeks'
function insert(str)
{
var subStr = 'for'
var pos = 5
document.write([str.slice(0, pos), subStr, str.slice(pos)].join(''))
}
insert(str)
</script>
24. Write a JavaScript function that checks whether a passed string is palindrome or not.
<script type="text/javascript">
let str = prompt("Enter a string")
str = str.toLowerCase()
let rev = ''
for(let i=str.length-1;i>=0;i--){
rev+=str[i]
}
if(rev==str)
document.write("Palindrome!")
else{
document.write("Not Palindrome!"+"<br>")
document.write(rev+"<br>")
document.write(str+"<br>") }</script>
4 marks
25. Differentiate between concat() and join() methods of array object.
26. Write a javascript program to demonstrate java intrinsic function.
27. Write and explain a string functions for converting string to number and number to
string.
<script>
var str = "10"
document.write("To int: "+parseInt(str)+"<br>");
var intr = 20
document.write("To String: "+intr.toString());
</script>
28. Differentiate between concat( ) & join( ) methods of array object.
29. Write a JavaScript function to check the first character of a string is uppercase or not.
<script>
function upper_case(str)
{
regexp = /^[A-Z]/;
if (regexp.test(str))
{
console.log("String's first character is uppercase");
}
else
{
console.log("String's first character is not uppercase");
}
}
upper_case('Abcd');
upper_case('abcd');
</script>
30. Write a JavaScript function to merge two array & removes all duplicate values.
<script>
const arr1 = [10, 20, 30, 40];
const arr2 = [20, 40, 60, 80];
const mix = arr1.concat(arr2);
const output = mix.filter(function(val, index){
return mix.indexOf(val) == index;
});
document.write(output);
</script>
31. Write a JavaScript program that will remove the duplicate element from an array.
<script>
const arr1 = [10,10,40, 20, 30, 40];
const output = arr1.filter(function(val, index){
return arr1.indexOf(val) == index;
});
document.write(output);
</script>
32. Write a javascript function that accepts a string as a parameter and find the length of
the string.
<script>
var str="welcome to javascript"
function findlength(s)
{
return(s.length)
}
document.write("Length="+findlength(str))
</script>
Write the use of chatAt() and indexof() with syntax and example
33. Differentiate between concat() and join() methods of array object
34. Write a JavaScript that will replace following specified value with another value in
string.
a. String = “I will fail” Replace “fail” by “pass”
<script>
var str = "I will fail";
var index = str.indexOf("fail")
var op = str.slice(0,index)
op = op+"pass"
document.write(op)
</script>
35. Write a Java Script code to display 5 elements of array in sorted order
<html>
<head>
<title>Array demo</title>
</head>
<body>
<script>
var a=new Array();
a[0]=20;
a[1]=13;
a[2]=5;
a[3]=2;
a[4]=8;
a.sort(function(a,b)
{
return a-b
}
);
for(var i=0;i<a.length;i++)
{
document.write(a[i]+”<br>”);
}
</script></body></html>

36. Write the use of charCodeAt() and from CharCode() method with syntax and
example.
37. Write JavaScript code to perform following operations on string. (Use split() method)
Input String: "Sudha Narayana Murthy"
Display output as
First Name: Sudha
Middle Name: Narayana
Last Name: Murthy
<script>
var str = " Sudha Narayana Murthy ";
var names = str.split(" ");
document.write(“First Name: “+names[0]+"<br>");
document.write(“Middle Name: “+names[1]+"<br>");
document.write(“Last Name: “+names[2]+"<br>");
</script>
38. Explain splice() method of array object with syntax and example.
39. Differentiate between push() and join() method of array object with respect to use,
syntax, return value and example.
40. Differentiate between concat() and join() methods of array object.
Sample paper
41. Write a JavaScript function to count the number of vowels in a given string
script>
const str = prompt("enter String")
function countvowels(string){
let vowels = 0;
for (let i = 0; i < string.length; i++) {
switch (string[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++
break;
}
}
document.write(vowels)
}
countvowels(str)
</script>

42. Write a JavaScript that find and displays number of duplicate values in an array.
<html>
<body>
<h3>Finding duplicate values in a JavaScript array</h3>
<p>Here, we will find the repeating values in the given array.</p>
<p>Original array: [6, 9, 15, 6, 13, 9, 11, 15]</p>
<p id="result"></p>
<script>
//simple traversal method
let array = [6, 9, 15, 6, 13, 9, 11, 15];
let index = 0, newArr = [];
const length = array.length; // to get length of array
function findDuplicates(arr) {
for (let i = 0; i < length - 1; i++) {
for (let j = i + 1; j < length; j++) {
if (arr[i] === arr[j]) {
newArr[index] = arr[i];
index++;
}
}
}
return newArr;
}
document.getElementById('result').innerHTML = 'Duplicate values are: <b>' +
findDuplicates(array) + '</b>';
</script>
</body>
43. Write a function that prompts the user for a color and uses what they select to set the
background color of the new webpage opened .
<html>
<head> </head>
<body></body>
<script>
let color = prompt("Enter any color");
var myNewWindow = window.open();
myNewWindow.document.body.style.background = color;
</script>
</html>

44. Develop JavaScript to convert the given character to Unicode and vice versa.
<script>
var chr = “abc”
var charcode = chr.charCodeAt(2)
document.write("converting char to code " +charcode+"<br>")
const char = String.fromCharCode(97)
document.write("converting code to char "+char)
</script>

6 marks
45. Write a javascript function to generate Fibonacci series till user defined limit.
<script>
const limit = prompt("enter the limit")
let fibonacci = 0;
let num1 = 0;
let num2 = 1;
document.write(num1+"<br>"+num2+"<br>")
for (let num = 0; num < parseInt(limit); num++) {
fibonacci = num1 + num2
document.write(fibonacci+"<br>")
num1 = num2
num2 = fibonacci
}
</script>

You might also like