john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

javascript continued

var num = 42;
num / 42;
num % 10;

var variable = "example"
variable.length;
variable.substring(0,2);
"coding rules".replace("coding","programming");
"code".toUpperCase();

confirm("continue?")  => Cancel and Ok
alert("field cannot be empty")

var response = prompt("Do you like me?");
if ( response === "yes" ){
  console.log("I like you too!");
}

var response = prompt("Enter a number");
response = +response;

var i;
for ( i=2; i>0; i-- )
{  console.log( "i is now equal to " + i );
}

var i = 0;
do {
  console.log("This is iteration " + (i + 1) + ".");
  i++;
}
while( i < 4 );


var fullName = "";
var name;
var firstLetter;

var fixName = function()
{
  firstLetter = name.substring(0, 1);
  name = firstLetter.toUpperCase() + name.substring(1);
  fullName = fullName + " " + name;
};

name = prompt("Enter your first name (all in lower case):");
fixName();
console.log("And your full name is:" + fullName);

var isMultipleOfThree = function ( x ) {
  return x % 3 === 0;
};
var isNotMultipleOfThree = function ( x ) {
  return !isMultipleOfThree( x );
};


var isDivisible = function (x, y) {
    if( x % y === 0 )
    {   return true;    }
    else
    {   return false;   }
};

var isNotDivisible = function (x, y) {
    return !isDivisible( x,y);
};

isDivisible( 4 , 2 );
isNotDivisible( 4 , 2 );


var power = function( base , exponent )
{
    if( exponent === 0 )
    {   return 1;}
    return base * power( base, exponent -1 );
};

var stack = [];
function power( base, exponent) {
    if( exponent < 0 ){        console.log( "Error: no negative exponents");    }
   else  if ( exponent === 0 ) {    return 1;  }
   else {
    stack[ exponent -1 ] = base * power(base, exponent - 1);
       return stack[ exponent -1 ];
   }
}

power( 2 , 3 );
for( var i = 0; i< stack.length; i++ )
{    console.log( stack[ i] );
}
stack.push(  7 );

function factorial( n ) {
  if (n < 0) {
    console.log( "Termination condition to prevent infinite recursion");
    return;
  }
  if (n === 0) {
    return 1;
  }
  return n * factorial(n -1);
}
factorial(-1);
factorial(5);

var multiples = [];
function multiplesOf( base, i ) {
  if( i < 0 ){
    console.log( "exponent cannot be less than 0" );
  }  else if ( i === 0 ) {
    console.log( multiples );
  }  else {
    multiples[i - 1] = base * i;
    multiplesOf( base , i-1 );
  }
}
multiplesOf(3 ,6 );

  • « Android ontouchevent read file
  • array quicksort override lessthan »

Published

May 10, 2012

Category

java-servlet

~271 words

Tags

  • continued 26
  • java-servlet 61
  • javascript 43