PDF In-Depth

JavaScript - Using Arrays

January 19, 2000

Advertisement
Advertisement
 

This code shows mathematical calculations, show and hide of fields, and alerts. Carl Orthlieb has lent a hand to clean it up. Read on and enjoy your fruit.

This code is based around a sale occuring in a fruit store. A random fruit and quantity is generated when the document is opened. Additionally the note tendered for payment is randomly selected from a list of valid notes. The budding fruit purchaser must calculate the correct amount of change or be chastised for their response.

The bulk of the code is located within the document level JavaScript code, with a small number of field level elements used to call document level functions.

The function DoItAgain () is called in document script so that when the document is opened the appropriate variables are initialized.

For those who are interested you can see the document code here.

QuantityArray = new Array(2,3,4,5,6,7,8,9) 
FruitArray = new Array("image.bananas", "image.apples", "image.pears", 
"image.plums", "image.potatoes", "image.tomatoes") 
PriceArray = new Array(10,20,30,40,50) 
NoteArray = new Array(5,10,20) 
KarlArray = new Array("an idiot", 
"mathematically challenged", 
"not fit for human consumption", 
"large in appetite and small in mind", 
"in need of a mental tune-up", 
"a kumquat", 
"very small in the brain department", 
"smelly" ); 

var iPriceIndex = 0; 
var iFruitIndex = 0; 
var iNoteIndex = 0; 
var iQuantityIndex = 0; 

function LetsDoItAgain() 
{ 
HideAllFruits(); 
HideAllNotes(); 
HideAnswers(); 
DisplayRandomFruit(); 
DisplayRandomQuantity(); 
DisplayRandomPrice(); 
DisplayRandomNote(); 
} 

function DisplayRandomQuantity() 
{ 
// Select a random number between 1 and "ArraySize" 
var iNumberOfQuantities = QuantityArray.length; 
var iQuantityToDisplay = Math.round(((iNumberOfQuantities-1) 
* Math.random())); 

// Display the selected quantity 
iQuantityIndex = iQuantityToDisplay; 

var quantity = this.getField("quantity") 
quantity.value = QuantityArray[iQuantityToDisplay]; 
} 

function DisplayRandomPrice() 
{ 
// Select a random number between 1 and "ArraySize" 
var iNumberOfPrices= PriceArray.length; 
var iPriceToDisplay = Math.round(((iNumberOfPrices-1) 
* Math.random())); 

// Display the selected price 
iPriceIndex = iPriceToDisplay; 

var price = this.getField("price"); 
price.value = PriceArray[iPriceToDisplay]; 
} 

function DisplayRandomNote() 
{ 
// Select a random number between 1 and "ArraySize" 
var iNumberOfNotes = NoteArray.length; 
var iNoteToDisplay = Math.round(((iNumberOfNotes-1) 
* Math.random())); 

// Display the selected note 
iNoteIndex = iNoteToDisplay; 

var note = this.getField("note." + NoteArray[iNoteToDisplay]); 
note.hidden = false; 
} 

function DisplayRandomFruit() 
{ 
// Select a random number between 1 and "ArraySize" 
var iNumberOfFruits = FruitArray.length; 
var iFruitToDisplay = Math.round(((iNumberOfFruits-1) 
* Math.random())); 

// Display the selected fruit 
iFruitIndex = iFruitToDisplay; 
var image = this.getField(FruitArray[iFruitToDisplay]); 
image.hidden = false; 
} 

function HideAllFruits() 
{ 
var images = this.getField("image") 
images.hidden = true 
} 

function HideAnswers() 
{ 
var images = this.getField("answer") 
images.value = "" 
} 

function HideAllNotes() 
{ 
var images = this.getField("note") 
images.hidden = true 
} 

function CalculateChangeRequired() 
{ 
var iTotalPrice = PriceArray[iPriceIndex] * QuantityArray[iQuantityIndex]; 
var iChangeRequired = (NoteArray[iNoteIndex] * 100 - iTotalPrice)/100; 
var iAnswer = this.getField("answer"); 
console.println("Price index: " + iPriceIndex); 
console.println("Quantity index: " + iQuantityIndex); 
console.println("Note index: " + iNoteIndex); 
console.println("Price per item ="+ PriceArray[iPriceIndex]); 
console.println("Quantity =" + QuantityArray[iQuantityIndex]); 
console.println("Total Price =" + iTotalPrice); 
console.println("Dollar Amount =" + NoteArray[iNoteIndex]); 
if (iAnswer.value != iChangeRequired) { 
var nKarlIndex = Math.round(((KarlArray.length - 1) * Math.random())); 
app.alert("The shopkeeper thinks that Karl is " + 
KarlArray[nKarlIndex] + 
", the correct answer is " + iChangeRequired); 
} else { 
app.alert("Correct, thanks for helping Karl!", 1); 
} 
} 

function CheckAnswer() 
{ 
CalculateChangeRequired(); 
} 

LetsDoItAgain(); 
PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Back to the past, 15 years ago! Open Publish 2002

Looking back to 2002, it's amazing how much of the prediction became a reality. Take a read and see what you think!

September 14, 2017
Platinum Sponsor





Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

Features

Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.