spacer
Yehuda Shiran April 3, 2000
Accelerated Math Operators
Tips: April 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management
JavaScript supports accelerated math operators, similarly to other programming languages such as C. The accelerated notation improves the program performance. For example, the statement:

a = a + 5;

can be written as:

a += 5;

All math operators can be accelerated. The statement:

b = b - 8;

can be written as:

b -= 8;

and:

c = c % 2;

can be written as:

c %= 2;

We can rewrite the persistent random banner's code with accelerated math operators:

function refreshDocJSTOD(frequency) {
  var max = tips.length;
  var dateObj = new Date();
  switch(frequency) {
    case "month": // 0 - 11
      tipIndex = dateObj.getMonth();
      break
    case "dayOfTheMonth": // 1 - 31
      tipIndex = dateObj.getDate() - 1 // 0 - 30
      break;
    case "dayOfTheWeek": // 0 - 6
      tipIndex = dateObj.getDay();
      break;
    case "hour": // 0 - 23
      tipIndex = dateObj.getHours();
      break;
    case "now": // Default
    default:
      tipIndex = getRandomIndex(max);
   }
 tipIndex %= max;
 document.write(tips[tipIndex]);
}

function getRandomIndex(max) {
 var randomNum = Math.random();
 randomNum *= max;
 randomNum = parseInt(randomNum);
 if(isNaN(randomNum)) randomNum = 0; // for Netscape
 return randomNum;
}


People who read this tip also read these tips:

Look for similar tips by subject:

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags