JavaScript – Using Arrays

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();

You May Also Like

About the Author: Planet PDF Team

Leave a Reply