0% found this document useful (0 votes)
3 views8 pages

JAVASCRIPT_QUESTIONS

The document contains multiple HTML snippets with JavaScript functions demonstrating various array manipulations, such as checking if an input is an array, cloning an array, retrieving the first and last elements, joining array elements into a string, inserting dashes between even numbers, sorting an array, finding the most frequent item, and swapping the case of characters in a string. Each snippet includes console log statements to display the results of the functions. Overall, the document serves as a practical guide for basic JavaScript array operations.

Uploaded by

anshikagkashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

JAVASCRIPT_QUESTIONS

The document contains multiple HTML snippets with JavaScript functions demonstrating various array manipulations, such as checking if an input is an array, cloning an array, retrieving the first and last elements, joining array elements into a string, inserting dashes between even numbers, sorting an array, finding the most frequent item, and swapping the case of characters in a string. Each snippet includes console log statements to display the results of the functions. Overall, the document serves as a practical guide for basic JavaScript array operations.

Uploaded by

anshikagkashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Q.

1)

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>JS Bin</title>

</head>

<body>

<script>is_array = function(input) {

if (toString.call(input) === "[object Array]")

return true;

return false;

};

console.log(is_array('w3resource'));

console.log(is_array([1, 2, 4, 0]));</script>

</body>

</html>

Q.2)

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Clone an array</title>

</head>

<body>

<script>const originalArray = [1, 2, 3];

const clonedArray = [...originalArray];


console.log(clonedArray);</script>

</body>

</html>

Q.3)

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Get the first 'n' elements of an array</title>

</head>

<body>

<script>first = function(array, n) {

if (array == null)

return void 0;

if (n == null)

return array[0];

if (n < 0)

return [];

return array.slice(0, n);

};

console.log(first([7, 9, 0, -2]));

console.log(first([],3));

console.log(first([7, 9, 0, -2],3));

console.log(first([7, 9, 0, -2],6));

console.log(first([7, 9, 0, -2],-3));</script>

</body>

</html>
Q.4)

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Get last and last n elements of an array</title>

</head>

<body>

<script>last = function(array, n) {

if (array == null)

return void 0;

if (n == null)

return array[array.length - 1];

return array.slice(Math.max(array.length - n, 0));

};

console.log(last([7, 9, 0, -2]));

console.log(last([7, 9, 0, -2],3));

console.log(last([7, 9, 0, -2],6));</script>

</body>

</html>

Q.5)

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>Join all elements of an array into a string</title>

</head>
<body>

<script>myColor = ["Red", "Green", "White", "Black"];

console.log(myColor.toString());

console.log(myColor.join());

console.log(myColor.join('+'));</script>

</body>

</html>

Q.6)

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>Insert dashes (-) between even numbers</title>

</head>

<body>

<script>var num=window.prompt();

var str = num.toString();

var result = [str[0]];

for(var x=1; x<str.length; x++)

if((str[x-1]%2 === 0)&&(str[x]%2 === 0))

result.push('-', str[x]);

else

result.push(str[x]);

}
}

console.log(result.join(''));</script>

</body>

</html>

Q.7)

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>Write a JavaScript program to sort the items of an array</title>

</head>

<body>

<script>var arr1=[-3,8,7,6,5,-4,3,2,1];

var arr2=[];

var min=arr1[0];

var pos;

max=arr1[0];

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

if (max<arr1[i]) max=arr1[i];

for (var i=0;i<arr1.length;i++)

for (var j=0;j<arr1.length;j++)

if (arr1[j]!="x")

if (min>arr1[j])
{

min=arr1[j];

pos=j;

arr2[i]=min;

arr1[pos]="x";

min=max;

console.log(arr2);</script>

</body>

</html>

Q.8)

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>Write a JavaScript program to find the most frequent item of an array. - w3resource</title>

</head>

<body>

<script>var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];

var mf = 1;

var m = 0;

var item;

for (var i=0; i<arr1.length; i++)

for (var j=i; j<arr1.length; j++)

{
if (arr1[i] == arr1[j])

m++;

if (mf<m)

mf=m;

item = arr1[i];

m=0;

console.log(item+" ( " +mf +" times ) ") ;</script>

</body>

</html>

Q.9)

<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<title>Swap the case of each character of a string</title>

</head>

<body>

<script>

function swapCase(inputString) {

var result = '';

for (var i = 0; i < inputString.length; i++) {

var char = inputString[i];

result += char === char.toUpperCase() ? char.toLowerCase() : char.toUpperCase();

}
return result;

console.log(swapCase('The Quick Brown Fox'));</script>

</body>

</html>

You might also like