Number object and Array object methods

Notes from May 02, 2005 class

Number objects

The toFixed() method chops off decimals after a certain number of digits
toFixed () only works for browsers > v.5

toFixed() is nice for financial calculations, where you only want two decimals.

var priceOfTea = 3.99;
var priceOfTeaInChina = priceOfTea / 100;
document.write("The price of tea is $" + priceOfTea +" a pound.<br />");
document.write("The price of tea in China is " + priceOfTeaInChina +" a pound.<br />");
priceOfTeaInChina = priceOfTeaInChina.toFixed(2);
document.write("That's "+priceOfTeaInChina + " a pound in dollars and cents without a zillion decimals.");

// toFixed() rounds a number up or down in the process.
// toFixed() can round from 0 decimals (makes it a whole number) to 20 decimal points

More fun with arrays

The .length property

Array.length counts how many items are in an array. Bear in mind that, if you're accessing items in the array by their index numbers, arrays start their index at zero, not one. If there are12 items in an array, for example, the last item has index value of 11, not 12.

The concat() method

The concat() method joins arrays together. It only works for browsers >= v.4

var statesOfMind = new Array("confused","bored","messed up","happy");
var adjectives = new Array("funny-looking","sloppy","pretty","overgrown");
var descriptions = statesOfMind.concat(adjectives);
document.write("People can feel " + statesOfMind + ".<br />");
document.write("Trucks can be " + adjectives + ".<br />");
document.write("People can be described as " + descriptions + ".<br />");

The slice() method

The slice() method copies part of an array. It works within an array just like substr() works within a string. It takes two parameters, the index number of the first item to copy from the string, and the index item of the first item you don't want to copy.

Put in only one number and it'll go from there till the end of the array.

var firstThree = adjectives.slice(0,3);
document.write("The first three items in the adjectives array are " + firstThree + ".<br />");

The join() method

The join() method converts an array into a single string. The parameter contains the string you want between each element. Since it's a string, the value of the parameter has to be in quotes.

document.write("Let's format this... <br />");
var statesOfMind = statesOfMind.join("<br />");
var adjectives=adjectives.join(", ");
var descriptions=descriptions.join(", ");
document.write("People can feel <br />" + statesOfMind + ".<br />");
document.write("Trucks can be " + adjectives + ".<br />");
document.write("People can be described as " + descriptions + ".<br />");

The sort() method

var statesOfMind = new Array("confused","bored","messed up","happy","agitated");
var adjectives = new Array("funny-looking","sloppy","pretty","overgrown","young");
var descriptions = statesOfMind.concat(adjectives);
var elementIndex;

descriptions.sort();
document.write("Here's an alphabetic list of descriptive words:<br />");

for (elementIndex = 0; elementIndex < descriptions.length; elementIndex++)
{
document.write(descriptions[elementIndex] + "<br />");
}

Sorts are case-sensitive. Upper-case is sorted first, then lower-case. Can you use toUpperCase or toLowerCase to fix this? Not easily--those are string functions, and sort() is an array function.

The reverse() method

Reverse the sort order via the reverse() method

var statesOfMind = new Array("confused","bored","messed up","happy","agitated");
var adjectives = new Array("funny-looking","sloppy","pretty","overgrown","young");
var descriptions = statesOfMind.concat(adjectives);
var elementIndex;

descriptions.sort();
descriptions.reverse();
document.write("Here's an alphabetic list of descriptive words:<br />");

for (elementIndex = 0; elementIndex < descriptions.length; elementIndex++)
{
document.write(descriptions[elementIndex] + "<br />");
}

Or, follow the lead from our book and get fancy by offering the user a chance to determine the sort order:

var statesOfMind = new Array("confused","bored","messed up","happy","agitated");
var adjectives = new Array("funny-looking","sloppy","pretty","overgrown","young");
var descriptions = statesOfMind.concat(adjectives);
var elementIndex;

var sortOrder= prompt("Enter 1 for alphabetic order, or -1 for reverse order", 1);

if (sortOrder == 1)
{
descriptions.sort();
document.write(descriptions.join("<br />"));
}
else if (sortOrder == -1)
{
descriptions.sort();
descriptions.reverse();
document.write(descriptions.join("<br />"));
}
else
{
document.write("Can you please follow directions and enter either 1 or -1?");
}