You are on page 1of 11

Click to reach!

Contents
1. To sort the data of an array ..........................................2
2. To reverse a given sting: ...............................................3
3. to count the number of vowels in the given string ......4
4. to display prime numbers less than 100.......................4
5. to calculate the sum of digits of a number: ..................5
6. To print largest and smallest element of an array........6
7. To check the given number is palindrome or not.........7
8. To swap the values of two variables.............................8
9. To calculate the sum of all even numbers up to 100 ...8
10. To display the largest among three given number.....9
Additional javascript program ........................................10

Code by Sameer !!
1. To sort the data of an array
var arr1=[-3,8,7,6,5,-4,3,2,1];

var arr2=[];

var min=arr1[0];

var pos;

var 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;

Code by Sameer !!
}

arr2[i]=min;

arr1[pos]="x";

min=max;

console.log(arr2);

2. To reverse a given sting:

function string_reverse(str)
{
return str.split("").reverse().join("");
}

console.log(string_reverse("Kathmandu"));
console.log(string_reverse("Country"));
console.log(string_reverse("Capital"));

Code by Sameer !!
3. to count the number of vowels in the given string

function vowel_Count(str)
{

return str.replace(/[^aeiou]/g, "").length;


}

console.log(vowel_Count("Javascript"));
console.log(vowel_Count("jonad james"));

4. to display prime numbers less than 100


let count=0

let i,j

for(j=2;j<=100;j++)

for( i=1;i<=j;i++)

if(j%i==0)

count++

if(count==2)

console.log(j)

count=0

Code by Sameer !!
5. to calculate the sum of digits of a number:
(pending)

Code by Sameer !!
6. To print largest and smallest element of an array.
let numArray = [95, 1, 75, 7, 12, 50, 3, 88]

numArray.slice().sort(function(a, b) {

return a - b

})

console.log('smallest: ' + numArray[0] + ', largest: ' + numArray[numArray.length


- 1])

Code by Sameer !!
7. To check the given number is palindrome or not.

<html>
<head>
<title>JavaScript program to Check a given number is Palindrome or Not</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter a number">
</td>
</tr>
<tr>
<td> <button onclick="palindrome ()" >Submit</button> </td>
</tr>
</table>
<div id="num" ></div>

</body>
<script type="text/javascript">
function palindrome()
{
var a,n,r = 0;
n = parseInt(document.getElementById ("first").value);
n1 = n;
while(n > 0)
{
a = n%10;
n = parseInt(n/10);
r = r*10+a;
}
if(n1 == r)
{
document.getElementById ("num").innerHTML = r+" is a palindrome ";
}
else
{
document.getElementById("num").innerHTML = r+" is not a palindrome";
}
}
</script>
</html>

Code by Sameer !!
8. To swap the values of two variables.

let x = 20;

let y = 40;

console.log("x=",x,"y=",y)

let temp;

temp = x;

x = y;

y = temp;

console.log("After swapping:")

console.log("x=",x,"y=",y)

9. To calculate the sum of all even numbers up to 100


let sum=0

for(i=1;i<=100;i++)

if(i%2==0)

sum=sum+i

console.log(sum)

Code by Sameer !!
10. To display the largest among three given number.
function max_of_three(x, y, z)
{
max_val = 0;

if (x > y)

max_val = x;

} else

max_val = y;

if (z > max_val)

max_val = z;

return max_val;

console.log(max_of_three(1,0,1));

console.log(max_of_three(0,-10,-20));

console.log(max_of_three(1000,510,440));

Code by Sameer !!
Additional javascript program
 Factorial using javascript
<!DOCTYPE html>
<html>
<head>
<title>Factorial of a number</title>
<button onClick = "func()"> Click to get factorial </button>
<p id="para"> </para>
<script>
function func() {
function fact(num) {
if (num < 0){
return -1;
}
else if(num == 0){
return 1;
}
else {
let result = 1;
for(var i = num; i > 1; i--){
result *= i;
};
return result;
}
};
const num = 4;
document.getElementById("para").innerHTML = fact(num);
};
</script>
</head>
</html>

Code by Sameer !!
Code by Sameer !!

You might also like