Showing posts with label JavaScript programs. Show all posts
Showing posts with label JavaScript programs. Show all posts

Wednesday, January 4, 2012

Start Pattern

function star1(){ //simple star ladder
for(var i=1; i<=5; i++)
{
document.write("</br>" );
for(var j=1; j<=i; j++)
{
document.write("*");
}
}
}
function star2() //triangle
{
var patt = "*";
var no = 10;
for(var i=1; i<=5; i++)
{
document.write("</br>");
for(var sp = no; sp>=1; sp--)
{
document.write("&nbsp;");
}
for (k = 1; k<=i; k++)
{
document.write(patt);
}
no--;
}
}
function star3() //reverse triangle
{
var pat = "*";
var no = 5;
for(var i=1; i<=5; i++)
{
document.write("</br>");
for(var sp = 1; sp<=i; sp++)
{
document.write("&nbsp;");
}
for(k=no; k>=1; k--)
{
document.write(pat);
}
no--;
}
}
view raw gistfile1.js hosted with ❤ by GitHub

Monday, January 2, 2012

Palindrome



This is a simple program to check whether a string or number is palindrome or not .
function palindrome( )
{
var value = "";
var giveme = prompt("enter any string or number" value);
var nlen = giveme.length;
var inst = "";
var inlst = "";
var rev;
inst+= giveme.substr(0, nlen-0);
inlst+= giveme.substr(0, nlen);
rev = strev(inlst);
if(inst == rev)
{
console.log("it is palindrome");
}
else
{
console.log(" oops not palindrome");
}
}
palindrome();
view raw gistfile1.js hosted with ❤ by GitHub

Prime Number


This is simple javascript program to find out Prime Numbers between 1-100 and then store into empty array.
function prime(i)
{
var j;
if(i==0 || i==1) { // zero or one are not primes so they shd be exluded.
return false;
}
for(j=2; j
{
if(i%j==0) // if reminder is zero then false. in loop beacuse check for all value 1-10.
{
return false; // otherwise return true.
}
}
return true;
}
function main() // main function which gives all prime bumber into an array.
{
var arr = new Array();
var result;
for(i=0; i&lt;100; i++)
{
result = prime(i); // get the returned value by fucntion into variable result.
if(result == true)
{
arr.push(i); // all true values push or add into empty array.
}
}
console.log(arr);
}
explanation : in this use of two function main and prime . main function is to get all the prime numbers from 1- 100 and store into a empty array using push method of array . push method is to store elements into array.
view raw gistfile1.js hosted with ❤ by GitHub

Sunday, January 1, 2012

string reverse

Its a simple string reverse program. without using string function.
function stringrev( )
{
var restr = "";
var stname = "";
var giveme = prompt("give me string", stname);
document.write("orignal string is :"+ " " +giveme);
var rev = giveme.length;
for(var i = rev-1; i &gt;-1; i--)
{
restr+= giveme.substr(i,1);
}
console.log(restr);
}
stringrev( );
view raw gistfile1.js hosted with ❤ by GitHub

Binary Sort


arr = new Array()
arr[0] = 'Nano';
arr[1] = 'Indica';
arr[2] = 'BMW';
arr[3] = 'Innova';
arr[4] = 'Toyoto';
arr[5] = 'Alto';
arr[6] = 'Maruti';
arr[7] = 'Spark';
arr[8] = 'Chevrolet spark';

function binarySearch(arr, key){
var left = 0;
var right = arr.length - 1;
while (left <= right){
var mid = parseInt((left + right)/2);
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return arr.length;
}
function search(){
var element= binarySearch(arr,'Innova');



document.write("The element you are searching is at the index number: "+ element);
}

Tuesday, December 27, 2011

javascript

 This is my progress log on java Script . I have started learning JavaScript and you can find simple programs created in JavaScript .