just playing with code

javascript examples

Just use your mental acumen to learn-by-example - see some code, execute it, check its output, figure out what is going on, fiddle with and change the code, check its output, and so on and so forth - learning as you go.

For newcomers, initially, the code may seem unitelligible, but persist and it should become clear. Experiment. Change it. Break it. Fix it. Play with it.

Start with a bit of Javascript - just to get the ball rolling. You can make use of the JS-EXEC, the Javascript Executor to try these snippets out and experiment with them further.

Simple arithmetic

function calcStuff(x, y) {
  result = "";
  result += "for x = " + x + " and y = " + y + "\n";

  result += "  x + y = " + (x + y) + "\n";
  result += "  x - y = " + (x - y) + "\n";
  result += "  x * y = " + (x * y) + "\n";
  result += "  x / y = " + (x / y) + "\n";
  result += "  x % y = " + (x % y) + "\n"; // remainder after division
  return result;
}
calcStuff(13, 3);
Output:
for x = 13 and y = 3 
  x + y = 16 
  x - y = 10 
  x * y = 39 
  x / y = 4.333333333333333 
  x % y = 1

More arithmetic

function calcStuff(x, y) {
  result = "";
  result += "for x = " + x + " and y = " + y + "\n";
  result += "  Math.min(x, y) = "  + Math.min(x, y) + "\n";
  result += "  Math.max(x, y) = "  + Math.max(x, y) + "\n";
  result += "  Math.floor(x/y) = " + Math.floor(x/y) + "\n";
  result += "  Math.ceil(x/y) = "  + Math.ceil(x/y) + "\n";
  result += "  Math.round(x/y) = " + Math.round(x/y) + "\n";
  return result;
}
calcStuff(13, 3);
Output:
for x = 13 and y = 3 
  Math.min(x, y) = 3 
  Math.max(x, y) = 13 
  Math.floor(x/y) = 4 
  Math.ceil(x/y) = 5 
  Math.round(x/y) = 4

Loops

Count up from 1 to 10.

function counting() {
  result = "";
  for(i=1;i<=10;i++) {
    result += i + " ";
  }
  return result;
}
counting();
function counting() {
  i = 1;
  result = "";
  while (i<=10) {
    result += i + " ";
    i++;
  }
  return result;
}
counting();
Output:
1 2 3 4 5 6 7 8 9 10 

Count down from 10 to 1.

function countdown() {
  result = "";
  for(i=10;i>=1;i--) {
    result += i + " ";
  }
  return result;
}
countdown();
function countdown() {
  result = "";
  for(i=0;i<10;i++) {
    result += 10-i + " ";
  }
  return result;
}
countdown();
Output:
10 9 8 7 6 5 4 3 2 1 

The first 10 even numbers.

function even_numbers() {
  result = "";
  for(i=1;i<=10;i++) {
    result += i*2 + " ";
  }
  return result;
}
even_numbers();
function even_numbers() {
  result = "";
  for(i=2;i<=20;i++) {
    if (i%2 == 0) {
      result += i + " ";
    }
  }
  return result;
}
even_numbers();
function even_numbers() {
  i = 1;
  result = "";
  while (i<=20) {
    if (i%2 == 0) {
      result += i + " ";
    }
    i++;
  }
  return result;
}
even_numbers();
Output:
2 4 6 8 10 12 14 16 18 20 

The sum of the first n numbers.

function sum_numbers(n) {
  result = 0;
  for(i=1;i<=n;i++) {
    result += i;
  }
  return "The sum of the first " + n + " numbers is " + result;
}
sum_numbers(100);
Output:
The sum of the first 100 numbers is 5050

The sum of convergent power fractions.

This example illustrates how the sum of

  1/a +1 a^2 + 1/a^3 + 1/a^4 + ...

tends towards 1/(a-1). (a is a non-zero integer)

function calc_sum(a, n) {
  result = 0;
  for(i=1;i<=n;i++) {
    result +=  1 / Math.pow(a, i);
  }
  return "The sum of 1/" + a + 
     " + 1/(" + a + "^2)" + 
     " + 1/(" + a + "^3)" + 
     " + 1/(" + a + "^4)" + 
     " + ..., for " + n +  " terms is approx. " + result;
}
calc_sum(2, 50);
Output:
The sum of 1/2 + 1/(2^2) + 1/(2^3) + 1/(2^4) + ... for 50 terms is approx. 0.9999999999999991

Date and Time

What's the date?

var now   = new Date()
var day   = now.getDate();
var month = now.getMonth()+1;
var year  = now.getYear();
year = year + 1900;  // a very sad state of affairs

result = "Date: " +  day + "/" + month + "/" + year + " (d/m/y)";
Output:
Date: 16/4/2008 (d/m/y)

What's the time?

var now = new Date()
var hour = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();

result = "Time: " +  hour + ":" + min + ":" + sec + " (h:m:s)";
Output:
Time: 21:29:26 (h:m:s)